Replace part of a string with another string

前端 未结 15 2323
终归单人心
终归单人心 2020-11-22 02:54

Is it possible in C++ to replace part of a string with another string?

Basically, I would like to do this:

QString string(\"hello $name\");
string.re         


        
15条回答
  •  伪装坚强ぢ
    2020-11-22 03:38

    This could be even better to use

    void replace(string& input, const string& from, const string& to)
    {
        while(true)
        {
            size_t startPosition = input.find(from);
            if(startPosition == string::npos)
                break;
            input.replace(startPosition, from.length(), to);
        }
    }
    

提交回复
热议问题