std::string ReplaceM(std::string& orignStr,const std::string& oldStr,const std::string& newStr)
{
size_t pos = 0;
std::string tmpStr = orignStr;
std::string::size_type newStrLen = newStr.length();
std::string::size_type oldStrLen = oldStr.length();
bool flag = true;
while(true)
{
pos = tmpStr.find(oldStr,pos);
if(pos == std::string::npos) break;
tmpStr.replace(pos,oldStrLen,newStr);
pos += newStrLen;
flag =false;
}
if(!flag)
return ReplaceM(tmpStr,oldStr,newStr);
return tmpStr;
}
递归求字符串替换C++源代码
最新推荐文章于 2025-12-06 09:52:47 发布
本文详细介绍了使用C++标准库实现字符串替换的功能,通过定义一个名为std::stringReplaceM的函数,该函数接受原始字符串、旧字符串和新字符串作为参数,并返回替换后的字符串。
3941

被折叠的 条评论
为什么被折叠?



