替换字符,如替换全部(字符串,开始,结束,"_","-")

Replace chars like replaceAll(string, start, end, '_', '-')

本文关键字:替换 结束 字符串 字符 全部 开始      更新时间:2023-10-16

我想将字符串text=s_o_m_e=text替换为text=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) {
        //...
    }
}

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

replaceAll(string, start, end, '_', '-');

向上:

std::replace(str.begin() + start, str.begin() + end, '_', '-');

谢谢,高炉

<algorithm>中有一个函数。

std::replace(str.begin(), str.end(), '_', '-');

使用 std::replace .以下是更多详细信息。