前言
在c语言中字符串是用字符数组来实现的,而对于应用层而言,将会多次使用字符串,如果在使用字符数组就会降低效率,所以在C++标准,用类重新定义了字符串。
一、string类
头文件#include<string>
注意:头文件没有.h
string支持字符串大小比较
string支持字符串的拼接
string支持字符串的查找和替换
string也具备数组的灵活性,可以使用[]来访问
1.构造方法
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1;//构造一个空的字符串
cout<<s1<<endl;
cout<<s1.size()<<endl;//调用字符串对象的函数求长度
cout<<s1.length()<<endl;//调用字符串对象的函数求长度
string s2="hello";//字符串初始化成hello
cout<<s2<<endl;//hello
string s3(s2);//将s3的内容初始化s2
cout<<s3<<endl;//hello
string s4(4,'a');//初始化s4为4个字符a
cout<<s4<<endl;//aaaa
string s5(s2,1,4);//将s5的内容初始化为s2下标为1开始长度为3的字符串
cout<<s5<<endl;//ello
string s6(s2,1,10);//当最后一个参数的大小大于s2的大小时就最多将从下标为1开始的所有拷贝
cout<<s6<<endl;//ello
cout<<s6.length()<<endl;//4
// char *p="hello world";C++编译器对类型有着严格的要求
//左边是char * 右边是const char*
const char *p="hello world";
string s7(p); //将s7的内容初始化为p的内容
cout<<s7<<endl;//hello world
return 0;
}
2.赋值方法
1.利用char*类型的变量和常量,以及char类型的变量和常量对其进行赋值。
2.利用对象的成员函数 assign
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1;//定义
s1="hello world";//赋值
string s2;
s2=s1;//赋值
cout<<s2<<endl;// hello world
s2='A';//虽然赋值成'A'但是编译器会将他转化成字符串"A"
cout<<s2<<endl; //A
// assign()进行赋值,用法类似于初始化字符串
s2.assign(s1);//将s2的值赋值为s1
cout<<s2<<endl;//hello world
s2.assign(3,'a');//将s2的赋值为3个字符a
cout<<s2<<endl;//aaa
s2.assign(s1,1,4);//将s2赋值为s1下标从1开始,长度为4的字符串
cout<<s2<<endl;//ello
return 0;
}
assign方法与初始化的方法类似。
3.求长度方法
利用成员函数size和length
cout<<s1.size()<<endl;//调用字符串对象的函数求长度
cout<<s1.length()<<endl;//调用字符串对象的函数求长度
4.字符串的拼接
C++中字符串的拼接有两种方式
1.用运算符‘+’;
2.用成员函数 append
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1="hello ";//赋值
string s2="world";
/*
string s3;
s3=s1+s2;
cout<<s3<<endl;//hello world
*/
s1.append(s2);//将s2的值追加到s1
cout<<s1<<endl;//hello world
s1.append(s2,1,3);//将s2下标从1开始,长度为4的字符串追加到s1
cout<<s1<<endl;//hello worldorl
s1.append(4,'a');//对s1追加4个字符a
cout<<s1<<endl;//hello worldorlaaaa
return 0;
}
append函数的返回是一个引用,而且append函数相当于追加,append函数的用法和之前讲过的用法类似。
4.字符串的比较
字符串的比较有两种方法:4.字符串的比较
1.可以使用=,>,<,!=等比较运算符,对string类型进行计较。
2.可以使用成员函数compare进行比较。
#include<iostream>
#include<string>
using namespace std;
int main(){
string s1="hello ";//赋值
string s2="hellow";
bool ret;
ret=s1>s2;
if(ret==true)
cout<<"s1>s2"<<endl;
else if(ret==false)
cout<<"s1<s2"<<endl;
int tem;
tem=s1.compare(s2);
if(tem>0)
cout<<"s1>s2"<<endl;
else if(tem==0)
cout<<"s1==2"<<endl;
else if(tem<0)
cout<<"s1<s2"<<endl;
//tem=s1.compare(s2,2,4);没有这个用法
tem=s1.compare("hellow");//将s1与hellow进行比较
cout<<tem<<endl;//-1
tem=s1.compare(1,2,s2);//将s1的下标为1长度为2的字符串与s2进行比较
cout<<tem<<endl;//-1
//tem=s1.compare(s1,s2,2,4);没有这个用法
tem=s1.compare(1,2,s2,1,2);//将s1从下标为1长度为2的字符串与s2下标为1长度为2的字符串进行比较
cout<<tem<<endl;//0
return 0;
}
运算符的返回类型是bool类型,返回true为真,返回值为false为假。
compare的返回有三中情况,等于0的情况相等,大于0就大于,小于0小于。compare的用法于前面的函数用法类似。
5.求子串和交换
子串:
用substr函数
string s2=s1.substr(2,4);求下标2开始长度为4的字符串
string s1="it is ok";
string s2=s1.substr(2,4);// is
交换:
用swap函数
string s1="it is ok";
string s3="hello world";
s1.swap(s3);
cout<<s1<<endl;//hello world
cout<<s3<<endl;//it is ok
6.查找和替换
1.查找
string类有一些查找子串和字符的成员函数,他们的返回值都是字符或者子串的在字符串的位置。如果找不到,则返回string::npos。string::npos是在string类中定义的一个静态常态。这些函数如下:
find:从前往后查找子串或字符出现的位置。
rfind:从后往前查找子串或字符出现的位置。
if(s1.find("e")!=string::npos){
cout<<s1.find("e")<<endl;//1
}
if(s1.rfind("l")!=string::npos){
cout<<s1.rfind("l")<<endl;//9
}
2.替换
replace成员函数可以对string对象中的子串进行替换,返回值为自身的引用。
string s1="hello world";
s1.replace(1,3,"12345",2,4);//将"12345"的子串从下标为2到4的子串去替换 s1下标为1到3
cout<<s1<<endl;//h345o world
string s2("12345");
s2.replace(2,3,5,'o');//将s2的子串从下标为2到3的子串去替换为5个'o'
cout<<s2<<endl;//12ooooo
int n=s2.find("ooooo");
s2.replace(n,5,"XXX");//将s2的子串从下标为n到5的子串去替换为'XXX'
cout<<s2<<endl; //12XXX
return 0;
7.删除和替换
删除:
erase成员函数可以删除string对象中的子串,返回值为对象自身的引用。列如:
string s1="hello world";
s1.erase(1,3);//删除s1下标为1到3的子串
cout<<s1<<endl;//ho world
s1.erase(5);//删除s1下标为5之后的子串
cout<<s1<<endl;//ho wo
插入字符串:
insert成员函数可以在string对象中插入一个字符串,返回自身引用。
string s1="hello world";
s1.insert(2,"123");//在s1下标为3插入字符串"123"
cout<<s1<<endl;//he123llo world
s1.insert(3,5,'X');//在s1下标为3插入5个'X'
cout<<s1<<endl;//he1XXXXX23llo world