Last active
February 26, 2017 18:23
-
-
Save dwburke/a04f3f156ee412147d6620615a46b75b to your computer and use it in GitHub Desktop.
c++ string pattern replacement
This file contains 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
// 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