How to find and replace string?

后端 未结 10 1036
感动是毒
感动是毒 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:12

    Not exactly that, but std::string has many replace overloaded functions.

    Go through this link to see explanation of each, with examples as to how they're used.

    Also, there are several versions of string::find functions (listed below) which you can use in conjunction with string::replace.

    • find
    • rfind
    • find_first_of
    • find_last_of
    • find_first_not_of
    • find_last_not_of

    Also, note that there are several versions of replace functions available from <algorithm> which you can also use (instead of string::replace):

    • replace
    • replace_if
    • replace_copy
    • replace_copy_if
    0 讨论(0)
  • 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).
    0 讨论(0)
  • 2020-11-27 04:19

    Try a combination of std::string::find and std::string::replace.

    This gets the position:

    std::string s;
    std::string toReplace("text to replace");
    size_t pos = s.find(toReplace);
    

    And this replaces the first occurrence:

    s.replace(pos, toReplace.length(), "new text");
    

    Now you can simply create a function for your convenience:

    std::string replaceFirstOccurrence(
        std::string& s,
        const std::string& toReplace,
        const std::string& replaceWith)
    {
        std::size_t pos = s.find(toReplace);
        if (pos == std::string::npos) return s;
        return s.replace(pos, toReplace.length(), replaceWith);
    }
    
    0 讨论(0)
  • 2020-11-27 04:20

    Here's the version I ended up writing that replaces all instances of the target string in a given string. Works on any string type.

    template <typename T, typename U>
    T &replace (
              T &str, 
        const U &from, 
        const U &to)
    {
        size_t pos;
        size_t offset = 0;
        const size_t increment = to.size();
    
        while ((pos = str.find(from, offset)) != T::npos)
        {
            str.replace(pos, from.size(), to);
            offset = pos + increment;
        }
    
        return str;
    }
    

    Example:

    auto foo = "this is a test"s;
    replace(foo, "is"s, "wis"s);
    cout << foo;
    

    Output:

    thwis wis a test
    

    Note that even if the search string appears in the replacement string, this works correctly.

    0 讨论(0)
提交回复
热议问题