Skip to content

Instantly share code, notes, and snippets.

@ZwodahS
Created May 29, 2014 09:14
Show Gist options
  • Save ZwodahS/e18f7a6e9ea6cd20156b to your computer and use it in GitHub Desktop.
Save ZwodahS/e18f7a6e9ea6cd20156b to your computer and use it in GitHub Desktop.
A simple find and replace for C++
std::string& replaceString(std::string& newString, const std::string& searchString, const std::string& replaceString, const bool& multipleReplace)
{
size_t index = newString.find(searchString);
if(multipleReplace)
{
while(index != std::string::npos)
{
// replace
newString.replace(index, searchString.size(), replaceString);
// start searching from the end of the replaceString, such that the replaceString will never be part of the search
index = newString.find(searchString, index + replaceString.size());
}
}
else
{
if(index != std::string::npos)
{
newString.replace(index, searchString.size(), replaceString);
}
}
return newString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment