自己在做的字符串的时候遇到要删除子串..
百度无果,论坛里到是有讨论到..
自己看评论写了一下..
其实挺简单的...
1、利用find函数找到首次出现的位置!!!记住是首次,后面的要用迭代删除..
2、用int pos保存位置...(找到则 pos > -1)
3、用erase函数删除即可!
string strT = "abcdecdfg";
cout << strT << endl;
int pos = strT.find("cd");
if (pos >-1)
{
strT.erase(pos,2);
}
cout << strT << " " << pos <<endl;
return 0;
删除字符用 ‘c’
erase参数:起始位置,删除长度...