Skip to content

Instantly share code, notes, and snippets.

@dwburke
Last active February 26, 2017 18:23
Show Gist options
  • Save dwburke/a04f3f156ee412147d6620615a46b75b to your computer and use it in GitHub Desktop.
Save dwburke/a04f3f156ee412147d6620615a46b75b to your computer and use it in GitHub Desktop.
c++ string pattern replacement
// from http://stackoverflow.com/questions/2896600/how-to-replace-all-occurrences-of-a-character-in-string
// std::string tmp = ReplaceAll(description, std::string("%"), name);
std::string ReplaceAll(std::string str, const std::string& from, const std::string& to) {
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
}
return str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment