QString strTest = "SHUT\nDOWN";
QStringList slstLines = strTest.split("\n");
在上面的示例中,我希望字符串列表包含两个条目,但它只包含 1,这与 strTest 相同......为什么拆分不起作用?
我也试过:
QStringList slstLines = strText.split(QRegExp("[\n]"), QString::SkipEmptyParts);
结果是一样的。
解决了:
QStringList slstLines = strTest.split("\\n");
试试这个代码:例如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" ]