C++字符串

C++ 字符串知识点

C++有两种类型的字符串表现形式

  1. C风格字符串
  2. C++引入的string类

C风格字符串

C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 \0 终止的一维字符数组。因此,一个以 null 结尾的字符串,包含了组成字符串的字符。

模板

#include<iostream>
#include<cstring>//c风格类头文件
using namespace std;

int main()
{
char name[size];//定义变量  注:空字符需计入size
cin.getline(name,size);//整行输入
/****/
return 0;
}

常用函数及目的

序号函数及目的
1strcpy(s1,s2); 复制字符串s2到s1
2stract(s1,s2) ;s1+s2 ; 连接字符串s2到s1的末尾
3strlen(s1); 返回字符串s1的长度
4strcmp(s1,s2); 比较两个字符串的字符大小,若s1=s2,返回0;若s1>s2,返回值大于0;若s1<s2返回值小于0
5strchr(s1,ch); 返回一个指针,指向字符串s1中字符ch第一次出现的位置
6strstr(s1,s2);返回一个指针,指向字符串s1中字符串s2第一次出现的位置

操作

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
	char s1[20] = "Happy";
	char s2[20] = "Birthday";
	char s3[20];
	char day;
	
	//复制 s1到 s3; 
	strcpy(s3,s1);
	cout<< s3<<endl;	
	
	//连接s1和s2;
	strcat(s1,s2);
	cout<<s1<<endl;
	 
	 //返回连接后s1字符串的长度
	 cout<<strlen(s1)<<endl;
	 
	 //比较字符串s2和s3的字符大小
	 cout<<strcmp(s2,s3)<<endl;
	
	return 0;
 } 

C++string 类

模板

#include<iostream>
#include<string>//string类头文件
using namespace std;

int main()
{
string s1;//定义变量
cin>>s1;//getline(cin,s1);
/****/
return 0;
}

string 类常用函数

  1. 字符串的长度 length() ;字符串中字符的数量size().
  2. 返回或修改字符串中某个位置的字符 at().
#include<iostream>
#include<iostream>
using namespace std;

int main()
{ 
string s1="Hello world"
cout<<s1.at(1)<<endl;
s1.at(1)='a';
cout<<s1<<endl;
return 0;
}
//结果为 e;Hallo world
  1. c_str()函数返回一个指向正规C字符串的指针
cout<<s1.c_str()<<endl;
  1. 比较两个字符串compare()
    s1=s2 返回值为0; s1>s2 返回值大于0; s1<s2 返回值小于0.
#include<iostream>
#include<string>
using namespace std;

int main()
{
   string s1="ABCDEF";
   string s2="ABCDFG.";
   cout<<s1.compare(s2)<<endl; //比较s1,s2两字符串
   cout<<s1.compare(0,4,s2)<<endl;//s1前四个字符与字符串s2比较
   cout<<s1.compare(0,4,s2,1,4)<<endl;//s1前四个字符与s2 1~4 字符比较
   return 0;
}
//结果为-1,-3,0
  1. copy()函数拷贝自己的n个字符到str中。
string s1="ABCDEF";
char  a[10]={0};
s1.copy(a,4,0);//4代表拷贝个数,0为起始位置;
cout<<a<<endl;
//结果为ABCD;
  1. 如果字符串为空则empty()返回真(true),否则返回假(false).
cout<<s1.empty()<<endl;
//若s1为空返回为1;非空返回为0;

7.insert()插入

string s1="Welcome to Nework";	
s2="Lisa ";
 s1.insert(0,"Lisa ");
 s1.insert(0,"Lisa ");//在第0个字符前插入字符“Lisa”;
 s1.insert(0,s2);//在s1字符串第0位前插入字符串s2; 
 s1.insert(0,s2,2,2);//在字符串的位置0插入字符串s2的子串(从位置2开始,长2个字符); 
 

8.max_size()函数返回字符串能保存的最大字符数.
9.替换replace()

string s1="Nice to meet you.";
	string s2="See you again";
	char *ch="I love China";
	s1.replace(1,2,"To");//把1~3的字符替换为"To"
	s1.replace(1,2,s2,3,3);//把s1中1~3的字符替换为s2中3~6的字符 ; 
	
//结果 NToe to meet you. 
//     N yoe to meet you.

10.resize()函数改变本字符串的大小到num, 新空间的内容不确定。也可以指定用ch填充。

s.resize(10) //将原字符串大小改变为10;

11.swap()函数把两个字符串交换。

 string first( "This comes first" );
    string second( "And this is second" );
    first.swap( second );
    cout << first << endl;
    cout << second << endl;
    //显示为 And this is second
    // This comes first

12.substr()返回本字符串的一个子串,从i开始,长n个字符。如果没有指定,将是默认值 string::npos。这样,substr()函数将简单的返回从i开始的剩余的字符串。

string s("What we have here is a failure to communicate");

    string sub = s.substr(21);

    cout << "The original string is " << s << endl;
    cout << "The substring is " << sub << endl;
    //结果显示为
    //The original string is What we have here is a failure to communicate
   // The substring is a failure to communicate

13.删除erase()
参数i 和 n 有默认值, 这意味着erase()可以这样调用:只带有i以删除i后的所有字符,或者不带有任何参数以删除所有字符.

 string s("So, you like donuts, eh? Well, have all the donuts in the world!");
    cout << "The original string is '" << s << "'" << endl;
  
    s.erase( 50, 14 );//删除位置50之后14位的字符
    cout << "Now the string is '" << s << "'" << endl;

    s.erase( 24 );//删除位置24之后所有的字符
    cout << "Now the string is '" << s << "'" << endl;

    s.erase();//删除所有字符
    cout << "Now the string is '" << s << "'" << endl;
    //结果显示为
   // The original string is 'So, you like donuts, eh? Well, have all the donuts in the world!'
    //Now the string is 'So, you like donuts, eh? Well, have all the donuts'
    //Now the string is 'So, you like donuts, eh?'
    //Now the string is ''

14.capacity()函数返回在重新申请更多的空间前字符串可以容纳的字符数. 这个数字至少与 size()一样大.
15.get_allocator()函数返回本字符串的配置器.
16.查找find()
返回str在字符串中第一次出现的位置(从i开始查找,长度为n)。如果没找到就返回string::npos.

#include<iostream>
#include<string>
using namespace std;

int main()
{
   string s1="Omega,you look so beautiful.";
   string s2="look"; 
   int n;
   n=s1.find(s2,0);//从位置0开始查找;
   n=s1.find(s2,0,15);//从位置0开始查找,长度为15 
   if(n!=string::npos)
   cout<<"Found look is at " <<n<<endl;
   else cout<<"Didn't find look"<<endl;
   return 0;
}

(1)find_first_of 查找在字符串中第一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,(最多查找n个)如果没找到就返回string::npos ;
(2)find_first_not _of 在字符串中查找第一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始(最多查找n个)。如果没找到就返回string::nops ;
(3)find_last_of 在字符串中查找最后一个与str中的某个字符匹配的字符,返回它的位置。搜索从index开始,最多搜索num个字符。如果没找到就返回string::nops ;
(4)find_last_not_of 在字符串中查找最后一个与str中的字符都不匹配的字符,返回它的位置。搜索从index开始,最多查找num个字符如果没找到就返回string::nops .

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值