Qt?QString的使用實(shí)現(xiàn)
QString則使用隱式共享,又稱回寫(xiě)復(fù)制。當(dāng)兩個(gè)對(duì)象共享同一份數(shù)據(jù)時(shí),數(shù)據(jù)內(nèi)容不改變,則不進(jìn)行數(shù)據(jù)的復(fù)制,即將深拷貝和淺拷貝結(jié)合起來(lái)使用。
QString使用時(shí)在連續(xù)的內(nèi)存塊上保存字符串,QString內(nèi)存分配策略如下:
- 范圍0~20:每次4個(gè)字符。
- 范圍20~4084:每次2倍。
- 范圍4084~∞:每次2048個(gè)字符。
+:連接字符串
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():開(kāi)頭插入字符串
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():移除兩端空白字符,使用單個(gè)空格字符“ ”替代
QString str = " This is test code. "; QString str1 = str.simplified(); //str = " This is test code. "
startsWith():檢查字符串是否以某個(gè)字符串開(kāi)頭,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():功能同上,檢查結(jié)尾。
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():判斷一個(gè)字符串是否出現(xiàn)過(guò)。
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&):比較兩個(gè)字符串,前小于后返回負(fù)值,相等返回0,大于返回正值。此比較時(shí)基于平臺(tái)相關(guān)的本地字符集。
qDebug()<<"Result:"<<QString::localeAwareCompare("good","bad");
//Result: 1
compare((const QString&, const QString&,Qt::CaseSensitivity):指定是否進(jìn)行大小寫(xiě)比較,用法同上。
<:比較是否小于,是則返回true。
<=:比較是否小于等于,是則返回true。
==:比較是否相等,是則返回true。
>=:比較是否大于等于,是則返回true。
>:比較是否大于,是則返回true。
......
toInt():轉(zhuǎn)整型。
toDouble():轉(zhuǎn)雙精度浮點(diǎn)型。
toFloat():轉(zhuǎn)浮點(diǎn)型。
toLong():轉(zhuǎn)長(zhǎng)整型。
toLongLong():轉(zhuǎn)64位長(zhǎng)整型。
......
QByteArray():QString會(huì)返回一個(gè)const char *的QByteArray,既可以存儲(chǔ)原始字節(jié),也可以存儲(chǔ)以“\0”結(jié)尾的8位字符串。
QString str = "I'm good man";
QByteArray ba = str.toAscii();
ba.append("yes,very good");
toAscii():返回一個(gè)ASCII編碼的8位字符串。
toLatin1():返回一個(gè)Latin-1(ISO8859-1)編碼的8位字符串。
toUtf8():返回一個(gè)UTF-8編碼的8位字符串,UTF-8是ASCII碼的超集,它支持整個(gè)Unicode字符集。
toLocal8Bit():返回一個(gè)系統(tǒng)本地編碼的8位字符串。
isEmpty():檢查是否空字符串。
isNull():檢查是否為空。
QString().isNull; //true
Qstring().isEmpty(); //true
QString("").isNull; //false
QString("").isEmpty(); //true
到此這篇關(guān)于Qt QString的使用實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Qt QString內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Qt?QtCreator添加自定義注釋的實(shí)現(xiàn)方法
在寫(xiě)代碼的時(shí)候我們?yōu)榱艘?guī)范化,一般會(huì)加文件注釋、類注釋和函數(shù)注釋,本文主要介紹了Qt?QtCreator添加自定義注釋的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
如何查看進(jìn)程實(shí)際的內(nèi)存占用情況詳解
本篇文章是對(duì)如何查看進(jìn)程實(shí)際的內(nèi)存占用情況進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言實(shí)現(xiàn)控制臺(tái)掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)控制臺(tái)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
opencv圖片的任意角度旋轉(zhuǎn)實(shí)現(xiàn)示例
這篇博客將介紹如何使用OpenCV旋轉(zhuǎn)圖像任意角度,實(shí)現(xiàn)各個(gè)角度的旋轉(zhuǎn),具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-06-06
深入學(xué)習(xí)C語(yǔ)言中常見(jiàn)的八大排序
排序編程中非?;A(chǔ)的的理論方法,雖然排序的方法多,但是理解起來(lái)并不難,它是最基本的,初學(xué)者一定要掌握的東西。本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值2021-11-11
C++實(shí)現(xiàn)判斷一個(gè)字符串是否為UTF8或GBK格式的方法
這篇文章主要介紹了C++實(shí)現(xiàn)判斷一個(gè)字符串是否為UTF8或GBK格式的方法,涉及C++針對(duì)字符編碼的遍歷、判斷、編碼轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11
C語(yǔ)言修煉之路靈根孕育源流出?初識(shí)C言大道生下篇
C語(yǔ)言是一門面向過(guò)程、抽象化的通用程序設(shè)計(jì)語(yǔ)言,廣泛應(yīng)用于底層開(kāi)發(fā)。C語(yǔ)言能以簡(jiǎn)易的方式編譯、處理低級(jí)存儲(chǔ)器。C語(yǔ)言是僅產(chǎn)生少量的機(jī)器語(yǔ)言以及不需要任何運(yùn)行環(huán)境支持便能運(yùn)行的高效率程序設(shè)計(jì)語(yǔ)言2022-03-03

