How to find and replace string?

后端 未结 10 1131
感动是毒
感动是毒 2020-11-27 03:51

If s is a std::string, then is there a function like the following?

s.replace(\"text to replace\", \"new text\");
10条回答
  •  醉话见心
    2020-11-27 04:17

    Yes: replace_all is one of the boost string algorithms:

    Although it's not a standard library, it has a few things on the standard library:

    1. More natural notation based on ranges rather than iterator pairs. This is nice because you can nest string manipulations (e.g., replace_all nested inside a trim). That's a bit more involved for the standard library functions.
    2. Completeness. This isn't hard to be 'better' at; the standard library is fairly spartan. For example, the boost string algorithms give you explicit control over how string manipulations are performed (i.e., in place or through a copy).

提交回复
热议问题