日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt学习笔记之QString

發(fā)布時(shí)間:2024/9/21 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt学习笔记之QString 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

標(biāo)準(zhǔn)C++提供了 兩種字符串。一是C風(fēng)格的以“\0”祝福語胡額為的字符數(shù)組,二是字符串類String。詳見《深入淺出之string》。

1. 字符串類QString

QString 類是 Qt 中用于表示字符串的類,實(shí)現(xiàn)在 QtCore 共享庫(kù)中。QString類保存了16位Unicode值,提供了豐富的操作、查詢、換換等函數(shù)。該類還進(jìn)行了使用隱私共享、高效的內(nèi)存分配策略等多方面的優(yōu)化。

Qstring(); // 構(gòu)造空字符串 QString(QChar ch); // 由 QChar 對(duì)象 ch構(gòu)造 QString(const QChar *pch, int size); // 由 QChar 數(shù)組pch構(gòu)造,size 是數(shù)組大小 QString(const QString &obj); // 拷貝構(gòu)造函數(shù) QString(const char *str); // 由字符串 str 構(gòu)造,str是一個(gè)普通字符串

2. 操作字符串

2.1 “+”操作符?

const QString operator+(const QString &s1, const QString &s2); const QString operator+(const QString &s1, const char *s2); const QString operator+(const char s1, const QString &s2); const QString operator+(const QString &s, char ch); #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello";str += " world";qDebug("%s",str.toStdString().data());return a.exec(); } 輸出結(jié)果是: hello world

2.2?append函數(shù)

#include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello";str.append(" world");qDebug("%s",str.toStdString().data());return a.exec(); } 輸出結(jié)果: hello world

2.3 sprintf函數(shù)

#include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str;str.sprintf("%s %s","hello","world");qDebug("%s",str.toStdString().data());return a.exec(); } 輸出結(jié)果: hello world

2.4 arg函數(shù)

#include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str;str = QString("%1 %2").arg("hello").arg("world");qDebug("%s",str.toStdString().data());return a.exec(); } 輸出結(jié)果: hello world

2.5 insert函數(shù)

//功能一般化的是在 QString 對(duì)象的任意位置插入另一個(gè)字符串或字符,如:QString &insert(int position, const QString &str); // 插入字符串 QString &insert(int position, const QChar *pch, int size); // 插入 QChar 數(shù)組 QString &insert(int position, QChar ch); // 插入 QChar 對(duì)象 //這里 position 參數(shù)是要插入的位置,返回值也是對(duì) QString 對(duì)象自己的引用。 #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str;str.insert(0,"hello");str.insert(str.count()," world");qDebug("%s",str.toStdString().data());return a.exec(); } 輸出結(jié)果: hello world

2.6 prepend函數(shù)?

?在原字符串的開頭插入另一個(gè)字符串

#include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str;str.prepend(" world");str.prepend("hello");qDebug("%s",str.toStdString().data());return a.exec(); } 結(jié)果輸出: hello world

2.7 replace函數(shù)

//以下是 QString 對(duì)象的替換操作,這三個(gè)函數(shù)的功能是將 QString 對(duì)象從 position 開始的 n 個(gè)字符替換為新內(nèi)容,新內(nèi)容分別由 QString 對(duì)象、QChar 數(shù)組 和 QChar 對(duì)象表示。QString &replace(int position, int n, const QString &after); // QString 對(duì)象 QString &replace(int position, int n, const QChar *pch, int size); // QChar 數(shù)組 QString &replace(int opsition, int n, QChar after); // QChar 對(duì)象 #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello world";str.replace(0,2,"world");qDebug("%s",str.toStdString().data());return a.exec(); } 結(jié)果輸出: worldllo world

?2.8 remove函數(shù)

//這個(gè)函數(shù)可以移除 QString 對(duì)象中從位置 position 開始的 n 個(gè)字符,下面兩個(gè)成員函數(shù)則可以從 QString 對(duì)象中移除指定的字符串或字符:QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive); QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive); #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello world";str.remove(0,2);qDebug("%s",str.toStdString().data());return a.exec(); } 輸出結(jié)果: llo world

?2.9 查找函數(shù)

//用以下的成員函數(shù)可以得到 QString 對(duì)象中某個(gè)特定字符串或字符出現(xiàn)的位置:這里參數(shù) from 是查找的起點(diǎn),它可以為負(fù)數(shù),-i 表示倒數(shù)第i個(gè)字符。查找的方向是從前往后。返回值是查找到的字符串或字符的位置,如果沒有找到則返回 -1。QString 類中還有與此功能相似的函數(shù)用于從后往前查找字符串或字符:int indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;int indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;int lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

2.10 清空

//下面這個(gè)成員函數(shù)可以清空一個(gè) QString 對(duì)象的內(nèi)容,使之成為空字符串。 void clear();

?2.11 移除兩端空白字符(包括回車符\n,換行符\r,制表符\t和空格符)

//下面這個(gè)成員函數(shù)可以截去 QString 對(duì)象中頭部和尾部的空白字符: //空白字符包括空格、回車、換行、制表符等字符。 QString trimmed() const; //下面這個(gè)成員函數(shù)不僅能去掉 QString 對(duì)象頭尾的空白字符,還能將中間的連續(xù)多個(gè)空白字符全部替換成一個(gè)空格: QString simlified() const;

2.12 大小寫轉(zhuǎn)換

QString toLower() const; // 轉(zhuǎn)換為小寫 QString toUpper() const; // 轉(zhuǎn)換為大寫

生活随笔為你收集整理的Qt学习笔记之QString的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。