How to replace all occurrences of a character in string?

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

    Old School :-)

    std::string str = "H:/recursos/audio/youtube/libre/falta/"; 
    
    for (int i = 0; i < str.size(); i++) {
        if (str[i] == '/') {
            str[i] = '\\';
        }
    }
    
    std::cout << str;
    

    Result:

    H:\recursos\audio\youtube\libre\falta\

提交回复
热议问题