SPlatten
SPlatten

Reputation: 5760

QString split in Qt 5.6

QString strTest = "SHUT\nDOWN";
QStringList slstLines = strTest.split("\n");

In the above example I would expect the String list to contain two entries, but it only contains 1, which is the same as the strTest...why isn't split working?

I've also tried:

QStringList slstLines = strText.split(QRegExp("[\n]"), QString::SkipEmptyParts);

The result is the same.

Upvotes: 4

Views: 2901

Answers (2)

proton
proton

Reputation: 341

Try this code: for example split:

#include <QString>
#include <QDebug>
...
QString str = "SHUT\nDOWN";
QStringList list = str.split("\n");
qDebug() << list;
//output: ("SHUT", "DOWN")

/////

QString str = "a\n\nb,\n";

QStringList list1 = str.split("\n");
// list1: [ "a", "", "b", "c" ]

QStringList list2 = str.split("\n", QString::SkipEmptyParts);
// list2: [ "a", "b", "c" ]

Upvotes: -1

SPlatten
SPlatten

Reputation: 5760

Solved:

    QStringList slstLines = strTest.split("\\n");

Upvotes: 3

Related Questions