Created
April 17, 2021 08:11
-
-
Save wangzhankun/03c1cd16be3a1888916a254248083db9 to your computer and use it in GitHub Desktop.
C++字符串中字符替换
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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