Yes: replace_all is one of the boost string algorithms:
Although it's not a standard library, it has a few things on the standard library:
More natural notation based on ranges rather than iterator pairs. This is nice because you can nest string manipulations (e.g., replace_all nested inside a trim). That's a bit more involved for the standard library functions.
Completeness. This isn't hard to be 'better' at; the standard library is fairly spartan. For example, the boost string algorithms give you explicit control over how string manipulations are performed (i.e., in place or through a copy).
Do we really need a Boost library for seemingly such a simple task?
To replace all occurences of a substring use this function:
If you need performance, here is an optimized function that modifies the input string, it does not create a copy of the string:
Tests:
Output:
like some say boost::replace_all
here a dummy example:
Yes:
replace_all
is one of the boost string algorithms:Although it's not a standard library, it has a few things on the standard library:
replace_all
nested inside atrim
). That's a bit more involved for the standard library functions.Not exactly that, but
std::string
has manyreplace
overloaded functions.Go through this link to see explanation of each, with examples as to how they're used.
Also, there are several versions of
string::find
functions (listed below) which you can use in conjunction withstring::replace
.Also, note that there are several versions of
replace
functions available from<algorithm>
which you can also use (instead ofstring::replace
):