Here is my code for finding a sequence in a string and replacing it with another:
std::string find_and_replace( string &source, string find, string replace )
{
size_t j;
for ( ; (j = source.find( find )) != string::npos ; )
{
source.replace( j, find.length(), replace );
}
return source;
}
Everything works fine when I call something like:
find_and_replace(test, "foo", "bar")
My application requires me to replace a single quote with two single quotes, not a double quote. For example I would call:
find_and_replace(test, "'", "''")
But whenever I call this, the function freezes up for some reason. Does anyone know what might be the cause of this problem?
Edit: based on the answers I've gotten, I have fixed the code:
std::string find_and_replace( string &source, string find, string replace )
{
string::size_type pos = 0;
while ( (pos = source.find(find, pos)) != string::npos ) {
source.replace( pos, find.size(), replace );
pos += replace.size();
}
return source;
}
I hope this helps some people having the same problem.