Replace part of a string with another string

前端 未结 15 2585
终归单人心
终归单人心 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:41

    This sounds like an option

    string.replace(string.find("%s"), string("%s").size(), "Something");

    You could wrap this in a function but this one-line solution sounds acceptable. The problem is that this will change the first occurence only, you might want to loop over it, but it also allows you to insert several variables into this string with the same token (%s)

提交回复
热议问题