QString則使用隐式共享,又稱回寫複制。當兩個對象共享同一份資料時,資料内容不改變,則不進行資料的複制,即将深拷貝和淺拷貝結合起來使用。
QString使用時在連續的記憶體塊上儲存字元串,QString記憶體配置設定政策如下:
- 範圍0~20:每次4個字元。
- 範圍20~4084:每次2倍。
- 範圍4084~∞:每次2048個字元。
+:連接配接字元串
QString str1 = "I ";
QString str2 = "LOVE ";
QString str3 = "YOU";
QString str4 = str1 + str2 + str3; //I LOVE YOU
append():追加字元串
QString str1 = "Hello ";
QString str2 = "World";
str1.append(str2);//Hello World
str1.append("!");//Hello World !
sprintf():指派/連接配接/追加字元串
QString str;
str.sprintf("%s","Nice to ");//str = Nice to
str.sprintf("%s %s","Nice to ", "meet you");//str = Nice to meet you
arg():同上
QString str;
str = QString("My name is %1, I'm %2 old.").arg("Paul").arg("17").
//str = My name is Paul, I'm 17 old.
insert():特定位置插入字元串
QString str = "This is test code.";
str.insert(8,"not ");
//str = This is not test code.
prepend():開頭插入字元串
QString str = "This is test code.";
str.prepend("Oh! ");
//str = Oh! This is test code.
replace():替換原字元串某些字元
QString str = "This is test code.";
str.replace(13,5,"data.");
//str = This is test data.
trimmed():移除字元串兩端的空白字元
QString str = " This is test code. ";
QString str1 = str.trimmed();
//str = This is test code.
simplified():移除兩端空白字元,使用單個空格字元“ ”替代
QString str = " This is test code. ";
QString str1 = str.simplified();
//str = " This is test code. "
startsWith():檢查字元串是否以某個字元串開頭,Qt::CaseSensitive指定
QString str = "This is test code.";
bool result = str.startsWith("This",Qt::CaseSensitive);
//result = true
QString str = "This is test code.";
bool result = str.startsWith("is",Qt::CaseSensitive);
//result = false
endsWith():功能同上,檢查結尾。
QString str = "This is test code.";
bool result = str.endsWith("code.",Qt::CaseSensitive);
//result = true
QString str = "This is test code.";
bool result = str.endsWith("is",Qt::CaseSensitive);
//result = false
contains():判斷一個字元串是否出現過。
【領QT開發教程學習資料,點選→Qt開發(視訊教程+文檔+代碼+項目實戰)←莬費領取,先碼住不迷路~】
QString str = "This is test code.";
bool result = str.contains("code.",Qt::CaseSensitive);
//result = true
QString str = "This is test code.";
bool result = str.contains("what",Qt::CaseSensitive);
//result = false
localeAwareCompare(const QString&, const QString&):比較兩個字元串,前小于後傳回負值,相等傳回0,大于傳回正值。此比較時基于平台相關的本地字元集。
qDebug()<<"Result:"<<QString::localeAwareCompare("good","bad");
//Result: 1
compare((const QString&, const QString&,Qt::CaseSensitivity):指定是否進行大小寫比較,用法同上。
<:比較是否小于,是則傳回true。
<=:比較是否小于等于,是則傳回true。
==:比較是否相等,是則傳回true。
>=:比較是否大于等于,是則傳回true。
>:比較是否大于,是則傳回true。
......
toInt():轉整型。
toDouble():轉雙精度浮點型。
toFloat():轉浮點型。
toLong():轉長整型。
toLongLong():轉64位長整型。
......
QByteArray():QString會傳回一個const char *的QByteArray,既可以存儲原始位元組,也可以存儲以“\0”結尾的8位字元串。
QString str = "I'm good man";
QByteArray ba = str.toAscii();
ba.append("yes,very good");
toAscii():傳回一個ASCII編碼的8位字元串。
toLatin1():傳回一個Latin-1(ISO8859-1)編碼的8位字元串。
toUtf8():傳回一個UTF-8編碼的8位字元串,UTF-8是ASCII碼的超集,它支援整個Unicode字元集。
toLocal8Bit():傳回一個系統本地編碼的8位字元串。
isEmpty():檢查是否空字元串。
isNull():檢查是否為空。
QString().isNull; //true
Qstring().isEmpty(); //true
QString("").isNull; //false
QString("").isEmpty(); //true