//input--原字符串
//find--被替换的子串
//replaceWith--替换字符串
string stringReplace(const string& input, const string &find, const string &replaceWith)
{
string strOut(input);
int curPos = 0;
int pos;
while ((pos = strOut.find(find, curPos)) != -1)
{
strOut.replace(pos, find.size(), replaceWith);
curPos = pos + replaceWith.size();
}
return strOut;
}
在网上搜索的时候看到好多人都使用了replace()/replaceAll()函数,但是我却不能用。不知道这些函数到底是标准库原来就有的,还是自己写的。
另外就是这里如果用char很不好用的。
比如说函数定义为
char* stringReplace(const int len)
{
char *strOut = new char[len];
return strOut;
}
//这是绝对错误的。但是有什么好的方法去避免吗?………………