子串
string s = "ABCDEFG";
cout<<s.substr(1,4);
//输出 BCD
查找
string s = "ABCDEFBG";
cout<<s.find("B");
cout<<s.find('B',2);//从第二个字符开始找
//输出 1和6
还有一些高级查找功能
find_first_not_of()
find_first_of()
find_last_of()
find_last_not_of()
(字符串切割\去除特定字符,比如空格) 把字符串变量转换成输入字符串流再赋值给多个变量,
比如“ABC,DEF,HIJ”这个变量要按‘,’切割成ABC DEF HIJ 三个变量
#include<bits/stdc++.h>
using namespace std;
int main(){
string st;
string s="ABC,EFG,HIJ";
stringstream input(s); // 字符串流
while (getline(input, st, ',')) cout<<st<<endl;
}
\\输出
ABC
EFG
HIJ