替换像replaceAll这样的字符(字符串,开头,结尾,'_',' - ')

Dug*_*las 1 c++ replace stl

我想替换字符串text=s_o_m_e=texttext=s-o-m-e=text

我有一个起始和结束索引:

std::string str("text=s_o_m_e=text");

std::string::size_type start = str.find("text="), end;

if (start != std::string::npos) {
    end = str.find("=", start);

    if (end != std::string::npos) {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)

所以,我正在寻找这样的函数:

replaceAll(string, start, end, '_', '-');
Run Code Online (Sandbox Code Playgroud)

UP:

std::replace(str.begin() + start, str.begin() + end, '_', '-');
Run Code Online (Sandbox Code Playgroud)

谢谢,Blastfurnace

Bla*_*ace 10

有一个功能<algorithm>.

std::replace(str.begin(), str.end(), '_', '-');
Run Code Online (Sandbox Code Playgroud)

  • @Duglas:你不想引用超过字符串的结尾.因为你正在使用偏移量,我想你想要`std :: replace(str.begin()+ start,str.begin()+ end,'_',' - ');` (4认同)

bes*_*and 5

使用std::replace.是更多细节.