;
当然,也可以使用类似 strcmp 的函数来进行 string 对象的比较,string 类提供的是 string.compare() 方法,函数原型如下:
int compare(const string&str) const;
int compare(size_t pos,size_t len,const string&str)const; // 参数 pos 为比较字符串中第一个字符的位置,len 为比较字符串的长度
int compare(size_t pos,size_t len,const string&str, size_t subpos,size_t sublen)const;
int compare(const char * s)const;
int compare(size_t pos,size_t len,const char * s)const;
int compare(size_t pos,size_t len,const char * s,size_t n)const;
compare 方法的返回值如下:
1)返回 0,表示相等;
2)返回结果小于 0,表示比较字符串中第一个不匹配的字符比源字符串小,或者所有字符都匹配但是比较字符串比源字符串短;
3)返回结果大于 0,表示比较字符串中第一个不匹配的字符比源字符串大,或者所有字符都匹配但是比较字符串比源字符串长。
六、使用 string.substr() 函数来获取子串
可以使用 string.substr() 函数来获取子串,string.substr() 函数的定义如下:
string substr(size_t pos = 0,size_t len = npos)const;
其中,pos 是子字符串的起始位置(索引,第一个字符的索引为 0),len 是子串的长度。这个函数的功能是:复制一个 string 对象中从 pos 处开始的 len 个字符到 string 对象 substr 中去,并返回 substr。
string str("Hello,World!");
string subStr = str.substr(3,5);
cout << subStr << endl;
这段代码的输出结果为:"lo,Wo"。
七、访问 string 字符串的元素
可以像 C 语言中一样,将 string 对象当做一个数组,然后使用数组下标的方式来访问字符串中的元素;也可以使用 string.at(index) 的方式来访问元素(索引号从 0 开始):
string str("Hello,World!");
cout << str[1] << endl; // 使用数组下标的方式访问 string 字符串的元素
cout << str.at(1) << endl; // 使用 at 索引访问 string 字符串的元素
八、string 对象的查找操作
8.1 使用 string.find() 方法查找字符
find 方法的函数原型如下:
1)从字符串的 pos 位置开始(若不指定 pos 的值,则默认从索引 0 处开始),查找子字符串 str。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回 string::npos:
size_type find (const string& str, size_type pos = 0) const;
2)从字符串的 pos 位置开始(若不指定 pos 的值,则默认从索引 0 处开始),查找子字符串 s。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回 string::npos:
size_type find (const char *s, size_type pos = 0) const;
3)从字符串的 pos 位置开始(若不指定 pos 的值,则默认从索引 0 处开始),查找 s 的前 n 个字符组成的子字符串。如果找到,则返回该子字符串首次出现时其首字符的索引;否则,返回 string::npos:
size_type find (const char *s, size_type pos, size_type n);
4)从字符串的 pos 位置开始(若不指定 pos 的值,则默认从索引 0 处开始),查找字符 ch 。如果找到,则返回该字符首次出现的位置;否则,返回 string::npos:
size_type find (char ch, size_type pos = 0) const;
举个查找子字符串的例子(查找字符的代码与这一样,只需要将 find 函数的参数换成字符即可):
#include <string>
#include <iostream>
using namespace std;
int main()
string str("cat,dog,cat,pig,little cat,hotdog,little pig,angry dog");
size_t catPos = str.find("cat",0);
if (catPos == string::npos) {
printf("没有找到字符串\n");
return 0;
while (catPos != string::npos) {
cout << "在索引 " << catPos << " 处找到字符串" << endl;
catPos = str.find("cat", catPos + 1);
return 0;
程序输出结果如下:
8.2 string.rfind()
string.rfind() 与 string.find() 方法类似,只是查找顺序不一样, string.rfind() 是从指定位置 pos (默认为字符串末尾)开始向前查找,直到字符串的首部,并返回第一次查找到匹配项时匹配项首字符的索引。换句话说,就是查找子字符串或字符最后一次出现的位置。还是以上面的程序为例,稍作修改:
#include <string>
#include <iostream>
using namespace std;
int main()
string str("cat,dog,cat,pig,little cat,hotdog,little pig,angry dog");
size_t catPos = str.rfind("cat",str.length()-1);
if (catPos == string::npos) {
printf("没有找到字符串\n");
return 0;
while (catPos != string::npos) {
cout << "在索引 " << catPos << " 处找到字符串" << endl;
catPos = str.rfind("cat", catPos - 1);
if (catPos == 0) {
cout << "在索引 " << catPos << " 处找到字符串" << endl;
break;
return 0;
程序输出结果如下:
可以看到,rfind 方法是从字符串末开始查找的。
8.3 string.find_first_of()
string.find_first_of() 方法在字符串中从指定位置开始向后(默认为索引 0 处)查找参数中任何一个字符首次出现的位置。举个例子说明:
#include <string>
#include <iostream>
using namespace std;
int main()
string str("cat,dog,cat,pig,little cat,hotdog,little pig,angry dog");
size_t pos = str.find_first_of("zywfgat");
if (pos == string::npos) {
printf("没有匹配到\n");
return 0;
cout << "在索引 " << pos << " 处匹配到" << endl;
return 0;
程序输出结果是:在索引 1 处匹配到。所查找的字符串 zywfgat 中,第一次出现在字符串 str 中的字符是 'a',该字符在 str 中的索引是 1.
8.4 string.find_last_of()
string.find_last_of() 方法在字符串中查找参数中任何一个字符最后一次出现的位置(也就是从指定位置开始往前查找,第一个出现的位置)。
8.5 string.find_first_not_of()
string.find_first_not_of() 方法在字符串中查找第一个不包含在参数中的字符。
8.6 string.find_last_not_of()
string.find_last_not_of() 方法在字符串中查找最后一个不包含在参数中的字符(从指定位置开始往前查找,第一个不包含在参数中的字符)。
九、string 对象的插入和删除操作
9.1 使用 string.insert() 进行插入操作
函数原型如下:
string&insert(size_t pos,const string&str); // 在位置 pos 处插入字符串 str
string&insert(size_t pos,const string&str,size_t subpos,size_t sublen); // 在位置 pos 处插入字符串 str 的从位置 subpos 处开始的 sublen 个字符
string&insert(size_t pos,const char * s); // 在位置 pos 处插入字符串 s
string&insert(size_t pos,const char * s,size_t n); // 在位置 pos 处插入字符串 s 的前 n 个字符
string&insert(size_t pos,size_t n,char c); // 在位置 pos 处插入 n 个字符 c
iterator insert (const_iterator p, size_t n, char c); // 在 p 处插入 n 个字符 c,并返回插入后迭代器的位置
iterator insert (const_iterator p, char c); // 在 p 处插入字符 c,并返回插入后迭代器的位置
举个例子:
#include <string>
#include <iostream>
using namespace std;
int main()
string str("abcdefgh");
str.insert(1,"INSERT"); // 在位置 1 处插入字符串 "INSERT"
cout << str << endl;
str.insert(10, 5, 'A'); // 在位置 10 处插入 5 个字符 'A'
cout << str << endl;
return 0;
输出结果如下:
9.2 使用 string.erase() 进行元素删除操作
函数原型如下:
string& erase (size_t pos = 0, size_t len = npos); // 删除从 pos 处开始的 n 个字符
iterator erase (const_iterator p); // 删除 p 处的一个字符,并返回删除后迭代器的位置
iterator erase (const_iterator first, const_iterator last); // 删除从 first 到 last 之间的字符,并返回删除后迭代器的位置
举个例子:
#include <string>
#include <iostream>
using namespace std;
int main()
string str("Hello,World!");
str.erase(5,6); // 删除从索引位置 5 开始的 6 个字符
cout << "str 为:" << str << endl;
return 0;
关于 erase() 函数的用法可以参考 https://www.cnblogs.com/liyazhou/archive/2010/02/07/1665421.html
十、string 对象的一些其他操作
10.1 使用 getline() 函数来获取 string 输入
string str;
getline(cin,str); // 从输入流中读取一行数据到 str
10.2 使用 empty() 函数判断字符串是否为空
string str;
if(str.empty()){
cout << "字符串为空" << endl;
string.empty() 函数,若字符串为空,则返回真,否则返回假。
10.3 使用 swap 函数交换两个字符串
#include <string>
#include <iostream>
using namespace std;
int main()
string str1 = "hello,world!";
string str2 = "HELLO,WORLD!";
str1.swap(str2);
cout << str1 << endl;
cout << str2 << endl;
return 0;