Skip to content

Instantly share code, notes, and snippets.

@wangzhankun
Created April 17, 2021 08:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wangzhankun/03c1cd16be3a1888916a254248083db9 to your computer and use it in GitHub Desktop.
Save wangzhankun/03c1cd16be3a1888916a254248083db9 to your computer and use it in GitHub Desktop.
C++字符串中字符替换
string replace(const string& old_value, const string& new_value, const string& str)
{
string ret = str;
replaceSrc(old_value, new_value, ret);
return ret;
}
inline void replaceSrc(const string& old_value, const string& new_value, string& str)
{
while(true) {
string::size_type pos(0);
if( (pos=str.find(old_value))!=string::npos )
str.replace(pos,old_value.length(),new_value);
else break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment