Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of str_replace in C++?

Tags:

c++

replace

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

like image 755
waiwai933 Avatar asked Jun 21 '10 00:06

waiwai933


2 Answers

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".

like image 87
Georg Fritzsche Avatar answered Sep 27 '22 15:09

Georg Fritzsche


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.

like image 36
Anthony Avatar answered Sep 27 '22 17:09

Anthony