views:

205

answers:

2

In PHP, there is a str_replace function that basically does a find and replace. Is there an equivalent of this function in C++?

+3  A: 

std::string::replace will do replacement. You can couple it with std::string::find* methods to get similar functionality. It's not as easy as the PHP way. I think boost has what you're looking for though; in regular expressions.

Duracell
+5  A: 

Not exactly, but take a look at the Boost String Algorithms Library - in this case the replace functions:

std::string str("aabbaadd");    
boost::algorithm::replace_all(str, "aa", "xx");

str now contains "xxbbxxdd".

Georg Fritzsche
no unicode? what?
Kugel
@Kugel: What are you aiming at? The string algorithms are abstract and work with any string fulfilling the [requirements](http://www.boost.org/doc/libs/1_43_0/doc/html/string_algo/design.html#string_algo.string). So go with `wstring`, use Boost.Locale with ICU or whatever fits best.
Georg Fritzsche
@Kugel: what do you mean?
Joe
Sorry I'm just upset with c++ lack of unicode support.
Kugel