In every language that I can think of, except C++, the function Replace essentially replaces all pieces of a string, whereas C++’s string class does not support simple operations like the following:
string s = "Hello World";
s = s.Replace("Hello", "Goodbye");
echo s; // Prints "Goodbye World"
This seems the most common use of any type of string replace function, but there doesn’t seem to be a standard replace function in C++. Am I missing something here?
EDIT: I’m aware that there’s not a built-in replace function like this in the standard library — I’m wondering if there is a more or less standard implementation made from standard algorithms or something of that sort.
You’re not missing anything, its not in the standard library.
You can either write that yourself using
find(),replace()etc. or use an implementation likereplace_all()from Boosts string algorithm library.