7

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.

2
  • I always had the same complaint. I don't think there's any solution in STL except to use find in conjunction with replace. Commented Feb 13, 2010 at 1:15
  • Similar question with more solutions: stackoverflow.com/questions/3418231/… Commented Feb 13, 2023 at 19:58

2 Answers 2

10

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 like replace_all() from Boosts string algorithm library.

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

Comments

0

this is not exactly the same but there is a replace method:

#include <iostream> #include <string> using namespace std; int main( void ) { string s = "Hello World"; s.replace(0, 5, "Goodbye"); cout << s << endl; return 0; } 

you can easily write your replacement using string::find, string::replace and a little loop

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.