int len = str.size(); int len = str.length(); //等价
string查找
1 2 3 4 5
string str = "This is a long string, and maybe you can find something here."; string sub1 = "something"; string sub2 = "anything"; int pos1 = str.find(sub1); int pos2 = str.find(sub2);
1 2
pos1 = 46; pos2 = -1;
迭代器
1 2 3 4 5 6 7 8 9
string str = "a simple test"; for(auto it = str.begin(); it != str.end(); ++it) cout << *it; cout << endl;
for(auto it = str.rbegin(); it != str.rend(); ++it) cout << *it; cout << endl;
for(auto it : s) cout << it; cout << endl;
1 2 3
a simple test tset elpmis a a simple test
empty()
检查字符串是否为空
clear()
清空字符串
insert(index, count, ch)
在index处插入count个字符ch
获取字符串子串
1 2 3 4 5
string s = "this is a test"; string s1 = s.substr(0, 4); string s2 = s.substr(10, string::npos);