8

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

1

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

2 Comments

@Kugel: What are you aiming at? The string algorithms are abstract and work with any string fulfilling the requirements. So go with wstring, use Boost.Locale with ICU or whatever fits best.
why would someone use boost for this basic functionality?
7

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.

Comments

0

You can also use std::regex_replace

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.