Qt学习笔记之QString
生活随笔
收集整理的這篇文章主要介紹了
Qt学习笔记之QString
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
標準C++提供了 兩種字符串。一是C風格的以“\0”祝福語胡額為的字符數組,二是字符串類String。詳見《深入淺出之string》。
1. 字符串類QString
QString 類是 Qt 中用于表示字符串的類,實現在 QtCore 共享庫中。QString類保存了16位Unicode值,提供了豐富的操作、查詢、換換等函數。該類還進行了使用隱私共享、高效的內存分配策略等多方面的優化。
Qstring(); // 構造空字符串 QString(QChar ch); // 由 QChar 對象 ch構造 QString(const QChar *pch, int size); // 由 QChar 數組pch構造,size 是數組大小 QString(const QString &obj); // 拷貝構造函數 QString(const char *str); // 由字符串 str 構造,str是一個普通字符串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(); } 輸出結果是: hello world2.2?append函數
#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(); } 輸出結果: hello world2.3 sprintf函數
#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(); } 輸出結果: hello world2.4 arg函數
#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(); } 輸出結果: hello world2.5 insert函數
//功能一般化的是在 QString 對象的任意位置插入另一個字符串或字符,如:QString &insert(int position, const QString &str); // 插入字符串 QString &insert(int position, const QChar *pch, int size); // 插入 QChar 數組 QString &insert(int position, QChar ch); // 插入 QChar 對象 //這里 position 參數是要插入的位置,返回值也是對 QString 對象自己的引用。 #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(); } 輸出結果: hello world2.6 prepend函數?
?在原字符串的開頭插入另一個字符串
#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(); } 結果輸出: hello world2.7 replace函數
//以下是 QString 對象的替換操作,這三個函數的功能是將 QString 對象從 position 開始的 n 個字符替換為新內容,新內容分別由 QString 對象、QChar 數組 和 QChar 對象表示。QString &replace(int position, int n, const QString &after); // QString 對象 QString &replace(int position, int n, const QChar *pch, int size); // QChar 數組 QString &replace(int opsition, int n, QChar after); // QChar 對象 #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(); } 結果輸出: worldllo world?2.8 remove函數
//這個函數可以移除 QString 對象中從位置 position 開始的 n 個字符,下面兩個成員函數則可以從 QString 對象中移除指定的字符串或字符: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(); } 輸出結果: llo world?2.9 查找函數
//用以下的成員函數可以得到 QString 對象中某個特定字符串或字符出現的位置:這里參數 from 是查找的起點,它可以為負數,-i 表示倒數第i個字符。查找的方向是從前往后。返回值是查找到的字符串或字符的位置,如果沒有找到則返回 -1。QString 類中還有與此功能相似的函數用于從后往前查找字符串或字符: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) const2.10 清空
//下面這個成員函數可以清空一個 QString 對象的內容,使之成為空字符串。 void clear();?2.11 移除兩端空白字符(包括回車符\n,換行符\r,制表符\t和空格符)
//下面這個成員函數可以截去 QString 對象中頭部和尾部的空白字符: //空白字符包括空格、回車、換行、制表符等字符。 QString trimmed() const; //下面這個成員函數不僅能去掉 QString 對象頭尾的空白字符,還能將中間的連續多個空白字符全部替換成一個空格: QString simlified() const;2.12 大小寫轉換
QString toLower() const; // 轉換為小寫 QString toUpper() const; // 轉換為大寫2.13 截斷
//而下面這個成員函數可以截斷 QStrring 對象,也就是去掉指定位置后的所有內容: void truncate(int position); // 從位置 position 截斷,位置從 0 開始編號 //下面這個成員函數可以截掉 QString 對象最后的若干個字符: void chop(int n); // 截掉最后的 n 個字符?3. 查詢字符串函數
?3.1 startsWith與endsWith函數
#include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello world";qDebug("%s",str.toStdString().data());qDebug("%d",str.contains("hello")); //! 是否以某字符串開頭qDebug("%d",str.endsWith("world")); //! 是否以某字符串結尾return a.exec(); }3.2 contains函數
//用以下的成員函數可以判斷 QString 對象是否包含指定的字符串或字符bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; bool contains(cosnt ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;3.3 比較字符串
//QString 類提供了一個函數用于兩個 QString 對象的比較: int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive); Qt::CaseInsensitive: //表示對大小寫不敏感 Qt::Casesensitive : //表示對大小寫敏感 //返回值的含義如下:大于 0 表示 s1 大于 s2,等于 0 表示 s1 等于 s2, 小于 0 表示 s1 小于 s2bool operator < (StringType other) const; // 比較是否小于 other bool operator <= (StringType other) const; // 比較是否小于等于 other bool operator == (StringType other) const; // 比較是否等于 other bool operator > (StringType other) constt; // 比較是否大于 other bool operator >= (StringType other) const; // 比較是否大于等于 other bool operator != (StringType other) const; // 比較是否不等于 other?4 字符串轉換
?4.1 將字符串轉換為其他類型
//注意: 當字符串以 0x開頭時,轉換的基自動轉換為16, 當字符串以0開頭時,轉換的基自動為8。 double toDouble(bool *ok = 0) const; // 轉換為高精度浮點數float toFloat(bool *ok = 0) cosnt; // 轉換為浮點數 int toInt(bool *ok, int base = 10) const; // 轉換為整型數 long toLong(bool *ok, int base = 10) cosnt; // 轉換為長整型 short toShort(bool *ok, int base = 10) const; // 轉換為短整型 uint toUInt(bool *ok = 0; int base = 10) const // 轉換為無符號整型數 ulong toLong(bool *ok = 0, int base = 10) const; // 轉換為無符號長整型數 ushort toUShort(bool *ok = 0, int base = 10) const; // 轉換為無符號短整型數 #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "123";qDebug("%d",str.toInt());return a.exec(); } 輸出結果是:1234.2 將QString類型轉換為QByteArray類型
toAscii()返回一個ASCII編碼的8位字符串(Qt5以上取消了) toLatin1()返回一個Latin-1編碼的8位字符串 toUtf8()返回一個Utf-8編碼的8位字符串 toLocal8Bit()返回一個本地local編碼字符串 #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello world";QByteArray ba = str.toLatin1();qDebug("%s",ba.data());return a.exec(); } 輸出結果: hello world5. 其他函數
5.1 null字符串與空字符串檢測
QString().isNull()//true QString().isEmpty()//true QString("").isNull()//fase QString("").isEmpty()//true5.2 索引
//QString 類也像普通的字符串一樣可以根據下標得到某個位置上的字符: const QChar at(int position) const;//更直觀的方法是用以下的操作符:const QChar operator[] (int position) const; const QChar operator[] (uint position) const; //上述QString 對象的取字符操作就類似于對一個字符數組的操作,但上述操作不能修改字符,事實上,通過[]操作符得到的字符還可以被修改,要用到另外兩個重載的[]操作符:QCharRef operator[] (int position);QCharRef operator[] (uint position); //返回的 QCharRef 類是一個輔助類,對它的修改將反映到原字符串中去。5.3、統計
以下兩個成員函數都可以得到 QString 對象中字符的個數,注意字符的個數并不一定等于字節數。 int size() const; int length() const; //用以下的成員函數可以得到 QString 對象包含某個特定字符串或字符的個數:int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const; #include <QCoreApplication> #include <QString> int main(int argc, char *argv[]) {QCoreApplication a(argc, argv);QString str = "hello world";qDebug("%d",str.size());qDebug("%d",str.length());qDebug("%d",str.count());return a.exec(); } 輸出結果: 11 11 115.4?截斷獲取子字符串?
QString left(int n) const; // 得到左邊 n 個字符構成的子字符串 QString right(int n) const; // 得到右邊 n 個字符構成的子字符串 QString mid(int position, int n = -1) const; // 從中間得到子字符串 //從中間得到子字符串時,參數 position 是子字符串的起始位置,參數 n 是字符串的個數,如果n 為 -1,則表示一直到原字符串的結尾。參考資料:
1.https://www.cnblogs.com/retry/p/9328731.html#qstring-對象的比較
總結
以上是生活随笔為你收集整理的Qt学习笔记之QString的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt学习笔记之类继承关系图
- 下一篇: Qt学习笔记之QByteArry