How to replace all occurrences of a character in string?

后端 未结 15 1249
别那么骄傲
别那么骄傲 2020-11-22 05:54

What is the effective way to replace all occurrences of a character with another character in std::string?

15条回答
  •  长情又很酷
    2020-11-22 06:28

    What about Abseil StrReplaceAll? From the header file:

    // This file defines `absl::StrReplaceAll()`, a general-purpose string
    // replacement function designed for large, arbitrary text substitutions,
    // especially on strings which you are receiving from some other system for
    // further processing (e.g. processing regular expressions, escaping HTML
    // entities, etc.). `StrReplaceAll` is designed to be efficient even when only
    // one substitution is being performed, or when substitution is rare.
    //
    // If the string being modified is known at compile-time, and the substitutions
    // vary, `absl::Substitute()` may be a better choice.
    //
    // Example:
    //
    // std::string html_escaped = absl::StrReplaceAll(user_input, {
    //                                                {"&", "&"},
    //                                                {"<", "<"},
    //                                                {">", ">"},
    //                                                {"\"", """},
    //                                                {"'", "'"}});
    

提交回复
热议问题