Skip to content

Instantly share code, notes, and snippets.

@Meraj
Last active February 9, 2021 18:29
Show Gist options
  • Save Meraj/6f08de97e3e232e26b763019074c68aa to your computer and use it in GitHub Desktop.
Save Meraj/6f08de97e3e232e26b763019074c68aa to your computer and use it in GitHub Desktop.
search in a string and replace all selected texts in c++
/**
* 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