Qt Split QString once

I want to split QString, but according to the documentation, the split function allows splitting whenever a character split occurs. I only want to split the place where the symbol first appears.

For instance:

5+6+7wiht default split()will end in a list containing["5","6","7"]

what i want: a list with two elements → ["5","6+7"]

Thanks in advance for your answers!

+4
source share
1 answer

There are several ways to achieve this, but this is probably and possibly the easiest:

main.cpp

#include <QString>
#include <QDebug>

int main()
{
    QString string("5+6+7");
    qDebug() << string.section('+', 0, 0) << string.section('+', 1);
    return 0;
}

main.pro

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

Assembly and launch

qmake && make && ./main

Output

"5" "6+7"
+10
source

All Articles