目录
1,insert
2,erase
3,find
4,replace
5,rfind
6,substr
7,find_first_of
8,find_first_not_of
9,find_last_of
10,operator+
11,getline
1,insert
在 pos 位置之前插入字符串
#include<iostream>
#include<string>
using namespace std;int main()
{string s1("hello world");s1.insert(0, "xx");cout << s1 << endl;s1.insert(0,2,'y');cout << s1 << endl;s1.insert(s1.begin(),'z');cout << s1 << endl;string s2("iiiiiiiiii");s1.insert(0,s2, 5);cout << s1 << endl;return 0;
}
2,erase
擦除范围字符串
int main()
{string s1("hello world");s1.erase(5, 4);cout << s1 << endl;s1.erase(1);cout << s1 << endl;return 0;
}
3,find
int main()
{string s1("hello world");size_t pos = s1.find('l',0);cout << s1[pos] << endl;pos = s1.find("ll", 1);cout << pos << endl;return 0;
}
4,replace
从 pos 位置开始,用 n 个字符替换;
int main()
{string s1("hello world");s1.replace(0, 2, "xx");cout << s1 << endl;s1.replace(0, 5,"yyy");cout << s1 << endl;return 0;
}
上述可以看到,第一个替换从下标 0 开始用两个字符也就是 ” he “ 替换 “ xx ” ;
第二个替换是从下标 0 开始用5个字符也就是 “ hello ” 替换 “ yyy ”;
给大家写一个替换字符的题目
将字符串中的空格都替换成其他字符串
int main()
{string s1("hello world hello bit");size_t pos = s1.find(" ", 0);while (pos != string::npos){s1.replace(pos, 1, "%20");pos = s1.find(" ", pos + 3);}cout << s1 << endl;return 0;
}
查找字符然后进行替换,然后在查找再替换直到查找不到为止退出;
5,rfind
从字符串pos位置开始往前找字符c,返回该字符在字符串中的位置
int main()
{string s1("hello world");size_t pos = s1.rfind('l',3);cout << pos << endl;pos = s1.rfind('l', 10);cout << pos << endl;pos = s1.rfind('o');cout << pos << endl;pos = s1.find("l");cout << pos << endl;pos = s1.rfind("l");cout << pos << endl;return 0;
}
6,substr
在 str 中从 pos 位置开始,截取 n 个字符,然后将其返回
int main()
{string s1("hello world");string s2=s1.substr(2, 3);cout << s2 << endl;string s3 = s1.substr(0);cout << s3 << endl;return 0;
}
我们再写一个查找后缀的程序;
int main()
{string s1("test.cpp");string s2("code.jbp");size_t pos = s1.rfind('.');if(pos != string::npos){string buff = s1.substr(pos);cout << buff << endl;}pos = s2.rfind('.');if(pos != string::npos){string buff = s2.substr(pos);cout << buff << endl;}return 0;
}
我们再写一个分离字符串的小程序
int main()
{string str("https://legacy.cplusplus.com/reference/string/string/substr/");size_t pos = str.find(':');string buff = str.substr(0, pos+1);cout << buff << endl;size_t pos1 = str.find('/',pos+3);buff = str.substr(pos + 1, pos1-pos);cout << buff << endl;size_t pos2 = str.rfind('/');buff = str.substr(pos1 + 1, pos2-pos1);cout << buff << endl;return 0;
}
7,find_first_of
直接看代码兄弟们
int main()
{string str("Please, replace the vowels in this sentence by asterisks.");size_t pos = str.find_first_of("abc");while (pos != string::npos){str.replace(pos, 1,"*");pos = str.find_first_of("abc",pos);}cout << str << endl;return 0;
}
8,find_first_not_of
与 find_first_of 功能相反,返回不属于字符串的下标
int main()
{string str("Please, replace the vowels in this sentence by asterisks.");size_t pos = str.find_first_not_of("abc");while (pos != string::npos){str.replace(pos, 1,"*");pos = str.find_first_not_of("abc",pos+1);}cout << str << endl;return 0;
}
9,find_last_of
跟 find_first_of 类似,只不过 find_last_of 是从后往前找的;
来个例子:
void SplitFilename(const std::string& str)
{std::cout << "Splitting: " << str << '\n';std::size_t found = str.find_last_of("/\\");std::cout << " path: " << str.substr(0, found) << '\n';std::cout << " file: " << str.substr(found + 1) << '\n';
}int main()
{std::string str1("/usr/bin/man");std::string str2("c:\\windows\\winhelp.exe");SplitFilename(str1);SplitFilename(str2);return 0;
}
10,operator+
int main()
{string s1("hello world");string s2("abcdefg");string s3 = s1 + s2;cout << s3 << endl;s1 = s2 + "666";cout << s1 << endl;s2 = "999" + s2;cout << s2 << endl;return 0;
}
11,getline
我们正常的输入是使用 cin
int main()
{string s1;//我们输入 hello worldcin >> s1;cout << s1 << endl;return 0;
}
但是 cin 遇到空格就会停下来,所以我们引入了 getline ;
int main()
{string s1;getline(cin, s1);cout << s1 << endl;cout << endl;//我们输入 hello worldcin >> s1;cout << s1 << endl;return 0;
}