/*
功 能:替换指定字符
参 数:str:字符串
oldValue:替换前字符
newValue:替换后字符
返回值:替换指定字符后的字符串
*/
string& ReplaceAll(string &str, const string& oldValue, const string& newValue)
{
string::size_type pos(0);
while ((pos = str.find(oldValue,pos)) != string::npos) //未找到返回npos,不等于就是找到
{
str.replace(pos,oldValue.length(),newValue);
if (newValue.length()>0)
{
pos += newValue.length();
}
}
return str;
}
C++ 面试题:字符串替换
最新推荐文章于 2022-09-01 11:37:50 发布