string 使用操作,引入 #inlcude<string>using namespace std;

  • 擦除元素
1
2
s.erase(0, 5);  // 整型参数:(起始位置, 长度)
s.erase(s.begin(), s.begin() + 5); // 迭代器类型:(起始位置, 结束位置)
  • 求子串
1
2
cout << s.substr(1, 3); // 整型参数:(起始位置, 长度) => 123
cout << s.substr(5); // 整型参数:(起始位置)到最后 => 56789
  • 查找子串
1
2
3
4
5
6
7
s.find('4');  // 查找字符, 返回找到的第一个字符'4'所在位置
s.find("345"); // 查找字符串,返回找到的第一个"345"子串所在位置
// 参数含义与上相同
s.find_first_of('1');
s.find_last_of('1');
s.find_first_not_of('1');
s.find_last_not_of('1');