【util】替换string中所有目标子串

来源:互联网 发布:c 二维数组 编辑:程序博客网 时间:2024/05/04 16:31

//将content中所有old_str替换为new_str的内容

void replaceAll(string& content, const string& old_str, const string& new_str)

{
    string::size_type pos = content.find(old_str), old_size = old_str.size(), new_size = new_str.size();
    while(pos != string::npos) 
    {
        content.replace(pos, old_size, new_str);
        pos = content.find(old_str, pos + new_size );
    }
}
0 0
原创粉丝点击