Skip to content

Instantly share code, notes, and snippets.

@liuml07
Last active April 23, 2016 12:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liuml07/5671365 to your computer and use it in GitHub Desktop.
Save liuml07/5671365 to your computer and use it in GitHub Desktop.
The missing std::string::replace_all(str, from, to)
size_t replace_all(string &str, const string &from, const string &to) {
size_t count = 0;
size_t pos = 0;
while ((pos = str.find(from, pos)) != string:npos) {
str.replace(pos, from.length(), to);
pos += to.length();
++count;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment