QString replace only first occurrence

12,004

Solution 1

You could try this:

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);

Resulting string will be:

that at a string

Solution 2

There is no convenience method for the operation you wish to have. However, you can use the following two methods to build your custom operation:

int QString::indexOf(const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns the index position of the first occurrence of the string str in this string, searching forward from index position from. Returns -1 if str is not found.

If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.

and

QString & QString::replace(int position, int n, const QString & after)

Replaces n characters beginning at index position with the string after and returns a reference to this string.

Note: If the specified position index is within the string, but position + n goes outside the strings range, then n will be adjusted to stop at the end of the string.

Now, putting all that into practice, you could write something as follows:

main.cpp

#include <QString>
#include <QDebug>

int main()
{
    QString initialString = QLatin1String("foo bar baz");
    QString fooString = QLatin1String("foo");
    initialString.replace(initialString.indexOf(fooString),
                          fooString.size(), QLatin1String("stuff"));
    qDebug() << initialString;
    return 0;
}

main.pro

TEMPLATE = app                                         
TARGET = main                                              
QT = core                                              
SOURCES += main.cpp

Build and Run

qmake && make && ./main

Output

"stuff bar baz" 
Share:
12,004
user3136871
Author by

user3136871

Updated on June 13, 2022

Comments

  • user3136871
    user3136871 almost 2 years

    Is there simple way of replacing only first occurrence of some substring by other substring in QString? It can be at any position.

  • SketchBookGames
    SketchBookGames over 9 years
    Resulting string should be: "this at a string"
  • Anon
    Anon over 8 years
    This does not work if you are trying to match a QRegularExpression, as it has no "size()".
  • Toby Speight
    Toby Speight almost 8 years
    Although this code may be help to solve the problem, providing additional context regarding why and/or how it answers the question would significantly improve its long-term value. Please edit your answer to add some explanation.
  • Toby Speight
    Toby Speight almost 8 years
    @Akiva, QRegExp has matchedLength(), and QRegularExpressionMatch has capturedLength() - what's the problem?
  • Sascha
    Sascha over 6 years
    @TobySpeight back references in newString (like \1) don't work, though.
  • kato2
    kato2 over 4 years
    @Akiva you can use pos and cap methods of the regular expression to get the text position and the text size. Just like I've explained in stackoverflow.com/questions/34612750/…