如何从一段由特殊符号分隔的 QString 中获取被分隔的子串?
- 从字符串 “one, two, three, four”中获取第二个由‘,’分隔的子串,即 “two” ;
#include <QtCore/QCoreApplication>
#include <QDebug>
int main()
{
QString str = "one, two, three, four";
qDebug<< str.section(',', 1, 1).trimmed()
return 0;
}
结果是 “two”,前后不包含空格。上面的函数 trimmed() 是去掉字符串前后的ASCII字符 '\t', '\n', '\v', '\f', '\r', and ' ',这些字符用QChar::isSpace()判断都返回true。