Last active
February 9, 2021 18:29
-
-
Save Meraj/6f08de97e3e232e26b763019074c68aa to your computer and use it in GitHub Desktop.
search in a string and replace all selected texts in 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
/** | |
* replace text in string | |
* @param str string | |
* @param from string | |
* @param to string | |
*/ | |
string replaceAll(std::string str,std::string from, std::string to) { | |
if(from.empty()) | |
return str; | |
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(); | |
} | |
return str; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment