QString仅替换第一次出现

use*_*871 6 c++ qstring qt qtcore

有没有简单的方法来替换QString中其他子字符串的第一次出现的某些子字符串?它可以在任何位置.

vah*_*cho 16

你可以试试这个:

QString str("this is a string"); // The initial string.
QString subStr("is"); // String to replace.
QString newStr("at"); // Replacement string.

str.replace(str.indexOf(subStr), subStr.size(), newStr);
Run Code Online (Sandbox Code Playgroud)

结果字符串将是:

那是一个字符串

  • @SketchBookGames,为什么?"这个"已经包含"是". (5认同)