字符串類(QString):
在Qt官方文檔中是這樣描述QString的:The QString class provides a Unicode character string.
我們可以將做C++中的string,但QString提供了更多有用的操作
在使用QString時需要包含頭文件 #include <QString>
QString的常見操作:
組合字符串:
1.使用二元操作符"+"組合兩個字符串
QString str1= "Hello", str2("World"); QString str3 = str1 + str2 + "!"; qDebug() << str3;
2.使用"+="操作符
QString str1 = "Hello"; str1 += " World!"; qDebug() << str1;
3.也可以使用QString::append()函數實現在一個字符串末尾添加一個字符串
QString str1 = "Hello"; str1.append(" World!"); qDebug() << str1;
4.使用QString::sprintf()也可以組合字符串,其支持的格式與C++中sprintf()函數定義的一樣
QString str1; str1.sprintf("%s", "Hello "); str1.sprintf("%s %s", "Hello ", "World!"); qDebug() << str1;
5.相比於QString::sprintf(),QString::arg()或許是個更好的選擇,該函數的重載可以處理很
多種數據類型,此外還提供額外的參數對字段寬度、數字基數、浮點數精度進行控制,且它類型
安全,完全支持Unicode,並且允許改變"%n"參數的順序。
QString str1; str1 = QString("%1 is a %2.").arg("Bob").arg("boy"); qDebug() << str1;
除了組合字符串之外,還提供了很多其他操作:
QString::insert():在指定位置后插入一個字符串
QString::prepend():在源字符串開頭插入一個字符串
QString::replace(): 用指定字符串代替源字符串中的指定內容
QString::trimmed():刪除字符串兩端的空白字符
QString::simplified(): 刪除字符串兩端的空白字符,並用單個空白字符代替字符串中出現的空白字符
注意:前面三個函數會對源字符串本身做出更改,而后兩個函數返回的是源字符串被操作后所
得內容的一個新的字符串,源字符串本身不會被修改
QString str1 = " AAA BBB CCC "; qDebug() << str1; str1.insert(2, "ZZZ"); qDebug() << str1; str1.prepend("DDD"); qDebug() << str1; str1.replace("AAA", "111"); qDebug() << str1; str1 = str1.trimmed(); qDebug() << str1; str1 = str1.simplified(); qDebug() << str1; /* 程序輸出: " AAA BBB CCC " " ZZZAAA BBB CCC " "DDD ZZZAAA BBB CCC " "DDD ZZZ111 BBB CCC " "DDD ZZZ111 BBB CCC" "DDD ZZZ111 BBB CCC" */
查詢字符串數據:
1.QString::startsWith()用於判斷字符串是否由指定字符串開頭,可以設置是否大小寫敏感(默認為大小寫敏感)
2.QString::endsWith()用於判斷字符串是否由指定字符串結尾,可以設置是否大小寫敏感(默認為大小寫敏感)
3.QString::contains()用於判斷字符串內是否含有指定子串
QString str1 = "Hello World!"; if (str1.startsWith("Hello", Qt::CaseSensitive)) qDebug() << "str1 start with Hello"; // 設為大小寫不敏感 if (str1.endsWith("world!", Qt::CaseInsensitive)) qDebug() << "str1 end with world"; if (str1.contains("Hello")) qDebug() << "str1 contains Hello"; /* 程序輸出: str1 start with Hello str1 end with world str1 contains Hello */
比較兩個字符串可以使用:
1.運算符:<、>、==、>=、<=
2.QString::localeAwareCompare(const QString &str1, const QString &str2);
比較兩個字符串,若str1大於str2,返回正整數,若str1小於str2,返回負整數,若str1等於str2,返回0
3.QString::compare(const QString &str1, const QString &str2, Qt::CaseSensitive/Qt::CaseInsensitive);
與QString::localeAwareCompare()功能相似,但卻可以設置是否大小寫敏感(默認敏感)
QString str1 = "AAA", str2 = "BBB", str3 = "Aaa"; if (str1 <= str2) qDebug() << str1 << "<=" << str2; else qDebug() << str1 << ">" << str2; int result = QString::compare(str1, str3, Qt::CaseInsensitive); if (result > 0) qDebug() << str1 << ">" << str3; else if (result < 0) qDebug() << str1 << "<" << str3; else qDebug() << str1 << "=" << str3; /* *輸出結果: * "AAA" <= "BBB" * "AAA" = "Aaa" */
將QString轉化為其他類型(此類函數有很多,下面只列出常用的幾個,若有需要,可以查看Qt文檔):
QString::toInt()
QString::toDouble()
QString::toFloat()
QString::toLong()
QString::toLongLong()
注意:
QString字符串的Empty與NULL,即默認構造函數創建QString與通過QString("")構造時
QString str1, str2(""); if (str1.isEmpty()) qDebug() << "str1 is Empty"; else qDebug() << "str1 is not Empty"; if (str1.isNull()) qDebug() << "str1 is NULL"; else qDebug() << "str1 is not NULL"; if (str2.isEmpty()) qDebug() << "str2 is Empty"; else qDebug() << "str2 is not Empty"; if (str2.isNull()) qDebug() << "str2 is NULL"; else qDebug() << "str2 is not NULL"; /* 輸出結果: str1 is Empty str1 is NULL str2 is Empty str2 is not NULL */
除了上面介紹的函數外,QString還有很多功能強大的函數,可以在Qt文檔或QCreator的Help中查看
