Qt中QStringList與QString的常用方法總結(jié)
本文匯集了QString 與 (QStringList | QByteArray)之間的轉(zhuǎn)換,以及QString、QStringList的一些常用方法。
QString 轉(zhuǎn) QByteArray
//method<1>
QString src1("hello kandy");
QByteArray ba1 = src1.toUtf8();
//method<2>
QString src2("hello kandy");
QByteArray ba2 = src2.toLatin1();
QByteArray 轉(zhuǎn) QString
//method<1>
QByteArray ba1("hello world");
QString str1 = ba1;
//method<2>
QByteArray ba2("hello world");
QString str2;
str2.prepend(ba2);
QStringList 轉(zhuǎn) QString
QStringList listemp;
listemp << "Apple" << "Grape" << "Orange" << "Banana";
QString str = listemp.join(",");
qDebug() << "\r\nstr:" << str;
//輸出結(jié)果如下
//str: "Apple,Grape,Orange,Banana"
QString轉(zhuǎn) QStringList
QString strtmp = "apple:5.99;grape:12.5;orange:6.99";
QStringList fruits = strtmp.split(";");
qDebug() << "\r\nfruits:" << fruits;
//fruits: ("apple:5.99", "grape:12.5", "orange:6.99")
//
for(int i=0; i < fruits.size(); i++)
{
QStringList fruit = fruits[i].split((":"));
qDebug() << "fruit:" << fruit;
/* fruit: ("apple", "5.99") fruit: ("grape", "12.5") fruit: ("orange", "6.99") */
}
QStringList 其他常用方法
//使用QStringList拆分QString
QString str = "hello,world,I,am,kandy";
QStringList strlist = str.split(",");
qDebug() << "strlist=" << strlist;
//輸出:strlist=("hello","world","I","am","kandy");
//判斷QStringList中是否包含某個(gè)成員
bool bcontain = strlist.contains("kandy");
qDebug() << "bcontain=" << bcontain;
//移除QStringList中的成員
strlist.removeOne("world");
qDebug() << "after removeOne, strlist=" << strlist;
QString 其他常用方法
//QString定義
QString s1 = "hello";
QString s2("world");
//字符串連接
QString str = s1 + s2;
//組包
str = QString("a=%1, b=%2, c=%3").arg("hello").arg("world").arg("12.34");
qDebug() << str;//"a=hello, b=world, c=12.34"
str = "a=hello, b=world, c=12.34";
//參數(shù)表示按逗號分隔,拆分出第0段到第1段
QString tmp = str.section(",", 0, 1);
qDebug() << "tmp=" << tmp;
//tmp= "a=hello, b=world"
//參數(shù)表示按逗號分隔,拆分出第0段到第0段
tmp = str.section(",", 0, 0);
qDebug() << "tmp=" << tmp;
//tmp= "a=hello"
//QString轉(zhuǎn)char *
//如果沒有中文(即字符串為ASCII):
QString ss1 = "hello test";
QByteArray byte1 = ss1.toLatin1();
char * szbuff1 = byte1.data();
//如果有中文(即字符串為Unicode):
QString ss2 = QString::fromStdWString(L"你好, 世界");
QByteArray byte2 = ss2.toLocal8Bit();
char * szbuff2 = byte2.data();
//替換QString中字符串
QString text("text-decoration: none");
text.replace("none", "underline");
qDebug() << "after replace, text=" << text;
//字符移除操作
QString sztmp = QString("hello,china").remove(2, 4); //從下標(biāo)2的字符開始, 移除4個(gè)字符
qDebug() << "sztmp=" << sztmp; //輸出hechina
//判斷字符是否以某字符或字符串開始/結(jié)束(startsWith/endsWith)
if (QString("@192.168.0.1#").startsWith("@"))
{
qDebug() << "startsWith(\"@\")";
}
if (QString("@192.168.0.1#").endsWith("#"))
{
qDebug() << "endsWith(\"#\")";
}
//判斷QString是否空
qDebug() << "QString().isEmpty()" << QString().isEmpty(); //true
qDebug() << "QString("").isEmpty()" << QString("").isEmpty(); //true
qDebug() << "QString(" ").isEmpty()" << QString(" ").isEmpty(); //false
qDebug() << "QString(\"hi\").isEmpty()" << QString("hi").isEmpty(); //false
qDebug() << "QString().isNull()" << QString().isNull(); //true
qDebug() << "QString("").isNull()" << QString("").isNull(); //false
qDebug() << "QString(" ").isNull()" << QString(" ").isNull(); //false
qDebug() << "QString(\"hi\").isNull()" << QString("hi").isNull(); //false
到此這篇關(guān)于Qt中QStringList與QString的常用方法總結(jié)的文章就介紹到這了,更多相關(guān)Qt QStringList QString內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
利用stream實(shí)現(xiàn)一個(gè)簡單的http下載器
這篇文章主要介紹了利用stream實(shí)現(xiàn)一個(gè)簡單的http下載器的相關(guān)資料,需要的朋友可以參考下2015-03-03
C語言實(shí)現(xiàn)任意進(jìn)制轉(zhuǎn)換器
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)任意進(jìn)制轉(zhuǎn)換器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
詳解c/c++賦值函數(shù)(重載=號運(yùn)算符)
大家都知道c++里的各種運(yùn)算符都是用函數(shù)實(shí)現(xiàn)的,比如=就等號函數(shù),所以當(dāng)用=給一個(gè)對象賦值的時(shí)候,實(shí)際調(diào)用的是=號所對應(yīng)的=號函數(shù)。下面通過本文給大家介紹c/c++賦值函數(shù)(重載=號運(yùn)算符),感興趣的朋友一起看看吧2018-08-08
解決C++ fopen按行讀取文件及所讀取的數(shù)據(jù)問題
今天小編就為大家分享一篇解決C++ fopen按行讀取文件及所讀取的數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07
C++容器std::vector的swap()函數(shù)使用方式
這篇文章主要介紹了C++容器std::vector的swap()函數(shù)使用方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
深入理解memmove()與memcpy()的區(qū)別以及實(shí)現(xiàn)方法
本篇文章是對memmove()與memcpy()的區(qū)別以及實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
簡單分析C語言中指針數(shù)組與數(shù)組指針的區(qū)別
這篇文章主要介紹了C語言中指針數(shù)組與數(shù)組指針的區(qū)別,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-11-11

