Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string::replace standard implementation?

Tags:

c++

string

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.

like image 401
Billy ONeal Avatar asked Feb 13 '10 01:02

Billy ONeal


People also ask

How do you replace part of a string with another string in C++?

Replace part of a string with another string in C++ In C++ the replacing is very easy. There is a function called string. replace(). This replace function replaces only the first occurrence of the match.

How do you replace a specific character in a string in C++?

To change a specific character in a string to another value, we refer to the index number position of the character in the string and use single quotation marks ( ' ' ). To access a character of a string in C++, we simply specify the index position of the character in the string in square brackets [] .

How do I replace str?

replace() Method. This method returns a new string resulting from replacing all occurrences of old characters in the string with new characters.

Which method is used to update a string with some replacement?

The replace() method returns a new string with one, some, or all matches of a pattern replaced by a replacement .


1 Answers

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.

like image 175
Georg Fritzsche Avatar answered Sep 28 '22 06:09

Georg Fritzsche