文章目录
1.1 string基本概念
- string是C++风格的字符串,而string本质上是一个类
- char * 是一个指针
- string是一个类,类内部封装了char*,管理这个字符串,是一个char*型的容器。
1.2 string构造函数
函数签名:
-
string();
:创建一个空的字符串string str;
-
string(const char* s);
: 使用字符串s初始化const char *s="hello"; string str(s);
-
string(const string& str);
: 使用一个string对象初始化另一个string对象string s="hello"; string str(s);
-
string(int n, char c);
: 使用n个字符c初始化string str(3,'h'); cout<<str<<endl;//hhh
1.3 string赋值操作
函数签名:
-
string& operator=(const char* s);
: char*类型字符串 赋值给当前的字符串const char * p="hello"; string str=p; //或者 string str="hello"; //"hello"本身就是const char *类型
-
string& operator=(const string &s);
: 把字符串s赋给当前的字符串string s="hello"; string str=s;
-
string& operator=(char c);
: 字符赋值给当前的字符串string str; str='a';// 赋值可以写str='a' 但是不能在初始化的时候直接string str='a';
-
string& assign(const char *s);
: 把字符串s赋给当前的字符串string str; str.assign("hello");
-
string& assign(const char *s, int n);
: 把字符串s的前n个字符赋给当前的字符串string str; str.assign("hello",3); cout<<str<<endl;//hel
-
string& assign(const string &s);
: 把字符串s赋给当前字符串string s="hello"; string str; str.assign(s); cout<<str<<endl;//hello
-
string& assign(int n, char c);
: 用n个字符c赋给当前字符串string str; str.assign(5,'h'); cout<<str<<endl;//hhhhh
1.4 string字符串拼接
-
string& operator+=(const char* str);
: 重载+=操作符string str="hello"; str+=" world";
-
string& operator+=(const char c);
: 重载+=操作符string str="hello"; str+='H';//也可以str+=97 但是实际上拼接的是将数字对应的ascii的字符
-
string& operator+=(const string& str);
: 重载+=操作符string str="hello"; string s=" world"; str+=s;
-
string& append(const char *s);
: 把字符串s连接到当前字符串结尾string str="hello"; str.append(" world");
-
string& append(const char *s, int n);
: 把字符串s的前n个字符连接到当前字符串string str="hello"; str.append("world",3); cout<<str<<endl;//hellowor
-
string& append(const string &s);
: 同operator+=(const string& str)string str="hello"; string s="world"; str.append(s); cout<<str<<endl;//helloworld
-
string& append(const string &s, int pos, int n);
: 字符串s中从pos开始的n个字符连接到字符串结尾string str="hello"; string s="world"; str.append(s,1,3); cout<<str<<endl;//helloorl
1.5 string查找和替换
-
int find(const string& str, int pos = 0) const;
: 查找str第一次出现位置,从pos开始查找string str="helloworldhello"; string sub="he"; int pos=str.find(sub,0); cout<<pos<<endl;//0 pos=str.find(sub,3); cout<<pos<<endl;//10
-
int find(const char* s, int pos = 0) const;
: 查找s第一次出现位置,从pos开始查找string str="helloworldhello"; int pos=str.find("he",0); cout<<pos<<endl;//0 pos=str.find("he",3); cout<<pos<<endl;//10
-
int find(const char* s, int pos, int n) const;
: 从pos位置查找s的前n个字符第一次位置string str="helloworldhello"; int pos=str.find("hello",0,2);//查找"he" cout<<pos<<endl;//0 pos=str.find("hello",3,2); cout<<pos<<endl;//10
-
int find(const char c, int pos = 0) const;
: 查找字符c第一次出现位置string str="helloworldhello"; int pos=str.find('h',0); cout<<pos<<endl;//0 pos=str.find('h',3); cout<<pos<<endl;//10
-
int rfind(const string& str, int pos = npos) const;
: 查找str最后一次位置,从pos开始查找string str="helloworldhello"; string sub="he"; int pos=str.rfind(sub,5);//5-->0寻找 cout<<pos<<endl;//0 pos=str.rfind(sub,12);//12-->0寻找 cout<<pos<<endl;//10
-
int rfind(const char* s, int pos = npos) const;
: 查找s最后一次出现位置,从pos开始查找string str="helloworldhello"; int pos=str.rfind("he",5);//5-->0寻找 cout<<pos<<endl;//0 pos=str.rfind("he",12);//12-->0寻找 cout<<pos<<endl;//10
-
int rfind(const char* s, int pos, int n) const;
: 从pos查找s的前n个字符最后一次位置string str="helloworldhello"; int pos=str.rfind("hello",5,3);//5-->0寻找"hel" cout<<pos<<endl;//0 pos=str.rfind("hello",12,4);//12-->0寻找"hell" cout<<pos<<endl;//10
-
int rfind(const char c, int pos = 0) const;
: 查找字符c最后一次出现位置string str="helloworldhello"; int pos=str.rfind('h',5);//5-->0 cout<<pos<<endl;//0 pos=str.rfind('h',12);//12-->0 cout<<pos<<endl;//10
-
string& replace(int pos, int n, const string& str);
: 替换从pos开始n个字符为字符串strstring str="helloworldhello"; string s="HELLO"; str.replace(0,5,s); cout<<str<<endl;//HELLOworldhello
-
string& replace(int pos, int n,const char* s);
: 替换从pos开始的n个字符为字符串sstring str="helloworldhello"; str.replace(0,5,"HELLO"); cout<<str<<endl;//HELLOworldhello
1.6 string字符串比较
-
字符串比较是按字符的ASCII码进行对比
= 返回 0
> 返回 1
< 返回 -1
-
int compare(const string &s) const;
: 与字符串s比较string s1="hello"; string s2="hello"; int cmp=s1.compare(s2); cout<<cmp<<endl;//0
-
int compare(const char *s) const;
: 与字符串s比较string s1="hello"; int cmp=s1.compare("hello"); cout<<cmp<<endl;
判断两个字符串是否相等经常使用的是==
string s1="hello";
int cmp=s1=="hello";
cout<<cmp<<endl;//1
1.7 string字符存取
-
char& operator[](int n);
: 通过[]方式取字符string s="hello"; cout<<s[0]<<end; s[0]='H'; //因为返回的是一个引用 所以可以作为左值进行赋值操作
-
char& at(int n);
: 通过at方法获取字符string s="hello"; cout<<s.at(0)<<endl; s.at(0)='H'; cout<<s.at(0)<<endl;
1.8 string插入和删除
-
string& insert(int pos, const char* s);
: 插入字符串string s="hello"; s.insert(1,"OOOO"); cout<<s<<endl;//hOOOOello
-
string& insert(int pos, const string& str);
: 插入字符串string s="hello"; string s1="OOOO"; s.insert(1,s1); cout<<s<<endl;//hOOOOello
-
string& insert(int pos, int n, char c);
: 在指定位置插入n个字符cstring s="hello"; s.insert(1,4,'O'); cout<<s<<endl;//hOOOOello
-
string& erase(int pos, int n = npos);
: 删除从Pos开始的n个字符string s="hOOOOello"; s.erase(1,4); cout<<s<<endl;//hello
1.9 string子串
-
string substr(int pos = 0, int n = npos) const;
: 返回由pos开始的n个字符组成的字符串string s="hello world"; string subStr=s.substr(1,3); cout<<subStr<<endl;//ell