V
V
Vova2018-11-28 13:09:59
C++ / C#
Vova, 2018-11-28 13:09:59

Why doesn't std::string have a human replace?

Subject.
I'm aware that there's a replace that takes a bunch of indexes, and the code looks like this:

std::string Text = "source";
std::string ToReplace = "so";
std::string ReplaceWith = "11";

Text.replace(Text.find(ToReplace),ToReplace.length(),ReplaceWith);

But it is somewhat strange that in the 21st century such a simple task requires so much writing.
Question one: why is it so?
It seems that C++ 11/14/17 has already appeared, but things are still there :(

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
Tesla4o, 2018-11-28
@Tesla4o

There is such a replase!

std::string str = "string test";
std::replace(str.begin(), str.end(), 's', 'd');

V
Victor Vakhturov, 2018-11-28
@xxx3Vxxx

#include <regex>
#include <string>
#include <iostream>

using namespace std;

int main()
{
    string t = "asd asd asd";
    t = regex_replace(t, regex("asd"), "sdf");
    cout << t;

    return 0;
}

This is of course from a cannon to sparrows, but as an option.
And why aren't there a lot of convenient methods...
The Standards Committee is quite conservative. The standard library has always been fairly minimalistic.

D
devalone, 2018-11-28
@devalone

STL has a lot of disadvantages, starting with the fact that there are no simple and useful functions, and ending with a very illogical code-style, as an option, you can use third-party libs like poco

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question