How to replace all occurrences of a character in string?

后端 未结 15 1273
别那么骄傲
别那么骄傲 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:27

    std::string doesn't contain such function but you could use stand-alone replace function from algorithm header.

    #include 
    #include 
    
    void some_func() {
      std::string s = "example string";
      std::replace( s.begin(), s.end(), 'x', 'y'); // replace all 'x' to 'y'
    }
    

提交回复
热议问题