c++替换string内所有符合条件的子串

std::size_t replaceAll(string& inout, string what, string with)
{
    std::size_t count{};
    for (std::string::size_type pos{};
         inout.npos != (pos = inout.find(what.data(), pos, what.length()));
         pos += with.length(), ++count) {
        inout.replace(pos, what.length(), with.data(), with.length());
    }
    return count;
}

 

posted on 2021-07-30 16:07  平ping  阅读(78)  评论(0)    收藏  举报