介紹QString較少使用卻頗有用的接口。
空白字符包含:'t', 'n', 'v', 'f', 'r'和' '。
接口:spa
QString QString::trimmed() const
示例:code
QString str = " lots\t of\nwhitespace\r\n "; str = str.trimmed(); // str == "lots\t of\nwhitespace"
QString str = " lots\t of\nwhitespace\r\n "; str = str.simplified(); // str == "lots of whitespace";
QStringList QString::split(const QString &sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QString str = "a,,b,c"; QStringList list1 = str.split(','); // list1: [ "a", "", "b", "c" ] QStringList list2 = str.split(',', QString::SkipEmptyParts); // list2: [ "a", "b", "c" ]
另外QStringList轉QString可使用如下接口:接口
QString QStringList::join(const QString &separator) const
void QString::truncate(int position)
示例:ip
QString str = "Hello world"; str.truncate(5); // str == "Hello"
int QString::count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const