sting下标从0开始
子字符串
substr(pos, len)
得到从 p o s pos pos开始长 l e n len len的字符串。
string s = "bcdefg";cout<<s.substr(2, 4);
// out: defg
插入
insert(pos, str_t)
在字符串 p o s pos pos前插入字符串str_t。
string s = "bcdefg";s.insert(0, "a");cout<<s;
// out: abcdefg
string s = "bcdefg";s.insert(4, "1234");cout<<s<<endl;
// out: bcde1234fg
删除
erase(pos, len)
从字符串 p o s pos pos开始删除 l e n len len个字符。
string s = "bcdefg";s.erase(1, 3);cout<<s;
// out: bfg
替代
replace(pos, len, str_t)
将字符串从 p o s pos pos开始长为 l e n len len的字符串替换为str_t。
string s = "bcdefg";s.replace(1, 3, "T");cout<<s;
// out: bTfg
查找
find(str_t, pos)
从字符串第 p o s pos pos位开始,找str_t字符串。
**如果找到,返回找到的首字符下标,否则返回string::npos。
static const size_type npos = static_cast<size_type>(-1);
string s = "bcdefg";int pos = s.find("de", 2);cout<<pos;
// out: 2
string s = "bcdefg";int pos = s.find("dae", 2);cout<<pos;
// out: -1
注意:
string s = "bcdefg";auto pos = s.find("dae", 2);cout<<pos;
// out: 18446744073709551615
/**这里是类型转换的缘故,返回-1,但是auto自动转为size_t类型导致的
*/