原文:http://blog.csdn.net/hmsiwtv/article/details/7553328
QString 類是 Qt 中用於表示字符串的類,實現在 QtCore 共享庫中。QString 類在實現上有以下特徵。
1)字符串採用 Unicode 內部編碼,可以表示世界上大多數語言的文字。
2)字符串的存儲有引用計數,當一個 QString 對象被複製爲另一個 QString 對象時,它們實際上指向相同的存儲空間,僅僅是增加一個引用計數。
3)採用 “按需複製” 的技術,當指向相同存儲空間的多個 QString 對象中的一個要被修改時,將真正複製一個新的字符串並進行修改。
構造
QString 類提供了很多不同原型的構造函數以方便使用。如:
- 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是一個普通字符串
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是一個普通字符串
由於存在這些構造函數,凡是可以用 QString 類作爲參數的地方,都可以安全地使用 QChar 對象或普通的字符串。
判斷
可以用下面的成員函數判斷 QString 對象是否爲空字符串:
- bool isEmpty() const; // 判斷是否爲空字符串
bool isEmpty() const; // 判斷是否爲空字符串
轉換
QString 類提供了很多函數用於將字符串轉換爲數值,如:
- 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; // 轉換爲無符號短整型數
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; // 轉換爲無符號短整型數
這些函數能夠解析 QString 對象的內容,將其轉換爲相應的數值。其中 ok 參數指向一個 bool 型變量, 這個參數用於輸出轉換是否成功的信息。base參數則是轉換爲整數類型時的基。這些函數都不會改變 QString 對象自身。
注意: 當字符串以 0x開頭時,轉換的基自動轉換爲16, 當字符串以0開頭時,轉換的基自動爲8。
下面這些成員函數可以將一個數值轉換爲字符串並設爲 QString 對象的值:
- QString &setNum(int n, int base = 10); // 整型數
- QString &setNum(uint n, int base = 10); // 無符號整型數
- QString &setNum(long n, int base = 10); // 長整型
- QString &setNum(ulong n, int base = 10); // 無符號長整型數
- QString &setNum(short n, int base = 10); // 短整型數
- QString &setNum(ushort n, int base = 10); // 無符號短整型數
- QString &setNum(double n, char format = 'g', int precision = 6); // 高精度浮點數
- QString &setNum(float n, char format = 'g', int precision = 6); // 浮點數
QString &setNum(int n, int base = 10); // 整型數
QString &setNum(uint n, int base = 10); // 無符號整型數
QString &setNum(long n, int base = 10); // 長整型
QString &setNum(ulong n, int base = 10); // 無符號長整型數
QString &setNum(short n, int base = 10); // 短整型數
QString &setNum(ushort n, int base = 10); // 無符號短整型數
QString &setNum(double n, char format = 'g', int precision = 6); // 高精度浮點數
QString &setNum(float n, char format = 'g', int precision = 6); // 浮點數
- QString number(int n, int base = 10);
- QString number(uint n, int base = 10);
- QString number(long n, int base = 10);
- QString number(ulong n ,int base = 10);
- QString number(double n, char format = 'q', int precision = 6);
QString number(int n, int base = 10);
QString number(uint n, int base = 10);
QString number(long n, int base = 10);
QString number(ulong n ,int base = 10);
QString number(double n, char format = 'q', int precision = 6);
這些函數都是靜態成員函數,因而與某個具體的對象無關,可以直接通過類名調用。
QString 類也提供了大小寫轉換的函數,如:
- QString toLower() const; // 轉換爲小寫
- QString toUpper() const; // 轉換爲大寫
QString toLower() const; // 轉換爲小寫
QString toUpper() const; // 轉換爲大寫
這些函數都不會改變 QString 對象本身,而是將轉換後的結果作爲返回值。
比較
QString 類提供了一個函數用於兩個 QString 對象的比較:
- int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive);
int compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive);
這是一個靜態成員函數,它可以比較 s1 和 s2 的大小,參數 cs 有以下兩個取值。
- Qt::CaseInsensitive: 表示對大小寫不敏感
- Qt::Casesensitive : 表示對大小寫敏感
Qt::CaseInsensitive: 表示對大小寫不敏感
Qt::Casesensitive : 表示對大小寫敏感
返回值的含義如下:大於 0 表示 s1 大於 s2,等於 0 表示 s1 等於 s2, 小於 0 表示 s1 小於 s2。
爲了方便使用,QString 類還提供了以下重載函數用於比較:
- int compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
int compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
這個函數用於比較 QString 對象自身和 QString 對象 other。 實際上更爲直觀的是使用 QString 類的比較操作符,如:
- bool 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
bool 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
這裏的 StringType 指的是 (const QString &)或 (const char *),哥也就是說,這些操作副既可以與 QString 對象比較,也可以與普通的字符串比較。它們的侷限性是第一個操作數必須是 QString 對象,因此,Qt 中又定義了以下操作符:
- bool operator < (const char *s1, const QString &s2); // 比較 s1 是否小於 s2
- bool operator <= (const char *s1, const QString &s2); // 比較 s1 是否小於等於 s2
- bool operator == (const char *s1, const QString &s2); // 比較 s1 是否等於 s2
- bool operator > (const char *s1, const QString &s2); // 比較 s1 是否大於 s2
- bool operator >= (const char *s1, const QString &s2); // 比較 s1 是否大於等於 s2
- bool operator != (const char *s1, const QString &s2); // 比較 s1 是否不等於 s2
bool operator < (const char *s1, const QString &s2); // 比較 s1 是否小於 s2
bool operator <= (const char *s1, const QString &s2); // 比較 s1 是否小於等於 s2
bool operator == (const char *s1, const QString &s2); // 比較 s1 是否等於 s2
bool operator > (const char *s1, const QString &s2); // 比較 s1 是否大於 s2
bool operator >= (const char *s1, const QString &s2); // 比較 s1 是否大於等於 s2
bool operator != (const char *s1, const QString &s2); // 比較 s1 是否不等於 s2
這些操作符不是 QString 類的成員,它們的第一個參數是普通字符串。
查找
用以下的成員函數可以判斷 QString 對象是否包含指定的字符串或字符:
- bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- bool contains(cosnt ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
bool contains(cosnt ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
用以下的成員函數可以得到 QString 對象包含某個特定字符串或字符的個數:
- int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
- int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
int count(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
int count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
用以下的成員函數可以得到 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 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;
這裏參數 from 是查找的起點,它可以爲負數,-i 表示倒數第i個字符。查找的方向是從前往後。返回值是查找到的字符串或字符的位置,如果沒有找到則返回 -1。
QString 類中還有與此功能相似的函數用於從後往前查找字符串或字符:
- 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
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
字符串處理
QString 類支持用賦值操作符進行對象的複製,其賦值操作符的聲明如下:
- QString &operator = (const QString &other); // 複製另外一個 QString 對象
- QString &operator = (const char *str); // 複製普通字符串
- QString &operator = (char ch); // 複製字符
- QString &operator = (QChar ch); // 複製 QChar 類對象
QString &operator = (const QString &other); // 複製另外一個 QString 對象
QString &operator = (const char *str); // 複製普通字符串
QString &operator = (char ch); // 複製字符
QString &operator = (QChar ch); // 複製 QChar 類對象
以下的成員函數可以將另一個字符串或字符接在 QString 對象後面,形成一個整體的字符串:
- QString &append(const QString &str); // 接續 QString 對象
- QString &append(const char *str); // 接續普通字符串
- QString &append(QChar ch); // 接續 QChar 對象
QString &append(const QString &str); // 接續 QString 對象
QString &append(const char *str); // 接續普通字符串
QString &append(QChar ch); // 接續 QChar 對象
它們的返回值是 QString 對象自己的引用,也就是說,可以用在這個返回值再次調用成員函數,形成連續的字符串接續操作。
爲了讓代碼更直觀, QString 類中還定義了一個操作符用於字符串的接續:
- QString &operator += (const QString &other); // 續接 QString 對象
- QString &operator += (const char *str); // 續接普通字符串
- QString &operator += (char ch); // 接續字符型數據
- QString &operator += (QChar ch); // 接續 QChar 對象
QString &operator += (const QString &other); // 續接 QString 對象
QString &operator += (const char *str); // 續接普通字符串
QString &operator += (char ch); // 接續字符型數據
QString &operator += (QChar ch); // 接續 QChar 對象
它們的功能與 append 相同。由於 C++ 語言允許賦值操作符和複合賦值操作符的返回值作爲左值使用,因此它們的返回值也被設計爲 QString 對象自己的引用,故也可以連續操作。但由於複合賦值操作符的結合順序是從右往左,要想先計算左邊的操作數需要加括號。
與 append 函數功能類似,以下的成員函數也能夠將另一個字符串或字符與 QString 對象連接起來,但是接在原字符串的前面:
- QString &prepend(const QString &str); // 在前面接續 QString 對象
- QString &prepend(const char *str); // 在前面接續普通字符串
- QString &prepend(QChar ch); // 在前面接續 QChar 對象
QString &prepend(const QString &str); // 在前面接續 QString 對象
QString &prepend(const char *str); // 在前面接續普通字符串
QString &prepend(QChar ch); // 在前面接續 QChar 對象
功能更一般化的是在 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 對象
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 對象自己的引用。
與插入相反的操作是移除 QString 對象中的一部分,如:
- QString &remove(int position, int n);
QString &remove(int position, int n);
這個函數可以移除 QString 對象中從位置 position 開始的 n 個字符,下面兩個成員函數則可以從 QString 對象中移除指定的字符串或字符:
- QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
- QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString &remove(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive);
QString &remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive);
以下是 QString 對象的替換操作:
- 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 對象
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 對象
這三個函數的功能是將 QString 對象從 position 開始的 n 個字符替換爲新內容,新內容分別由 QString 對象、QChar 數組 和 QChar 對象表示。
以下成員函數則可以搜索指定的字符串或字符並開始替換:
- QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // QString 替換爲 QString
- QString &replace(QChar ch, cosnt QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // QChar 替換爲 QString
- QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // Qchar 替換爲 QChar
QString &replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // QString 替換爲 QString
QString &replace(QChar ch, cosnt QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // QChar 替換爲 QString
QString &replace(QChar before, QChar after, Qt::CaseSensitivity cs = Qt::CaseSensitive); // Qchar 替換爲 QChar
下面這個成員函數可以清空一個 QString 對象的內容,使之成爲空字符串。
- void clear();
void clear();
而下面這個成員函數可以截斷 QStrring 對象,也就是去掉指定位置後的所有內容:
- void truncate(int position); // 從位置 position 截斷,位置從0開始編號
void truncate(int position); // 從位置 position 截斷,位置從0開始編號
下面這個成員函數可以截掉 QString 對象最後的若干個字符:
- void chop(int n); // 截掉最後的 n個字符
void chop(int n); // 截掉最後的 n個字符
以下幾個成員函數可以得到 QString 對象的子字符串:
- QString left(int n) const; // 得到左邊 n 個字符構成的子字符串
- QString right(int n) const; // 得到右邊 n 個字符構成的子字符串
- QString mid(int position, int n = -1) const; // 從中間得到子字符串
QString left(int n) const; // 得到左邊 n 個字符構成的子字符串
QString right(int n) const; // 得到右邊 n 個字符構成的子字符串
QString mid(int position, int n = -1) const; // 從中間得到子字符串
從中間得到子字符串時,參數 position 是子字符串的起始位置,參數 n 是字符串的個數,如果n 爲 -1,則表示一直到原字符串的結尾。
注意上述三個函數並不修改 QString 對象自身,而是返回一個臨時對象以供使用。
下面這個成員函數可以截去 QString 對象中頭部和尾部的空白字符:
- QString trimmed() const;
QString trimmed() const;
空白字符包括空格、回車、換行、製表符等字符。下面這個成員函數不僅能去掉 QString 對象頭尾的空白字符,還能將中間的連續多個空白字符全部替換成一個空格:
- QString simlified() const;
QString simlified() const;
加法操作符可以將兩個字符串或字符連接起來並以 QString 臨時對象的方式返回:
- 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);
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);
注意加法操作符的兩個操作數中必須至少有一個是 QString 對象,否則無法重載操作符。顯然,加法操作副都不是 QString 類的成員。
索引
QString 類也像普通的字符串一樣可以根據下標得到某個位置上的字符:
- const QChar at(int position) const;
const QChar at(int position) const;
這是一個成員函數,更直觀的方法是用以下的操作符:
- const QChar operator[] (int position) const;
- const QChar operator[] (uint position) const;
const QChar operator[] (int position) const;
const QChar operator[] (uint position) const;
這樣對 QString 對象的取字符操作就類似於對一個字符數組的操作。事實上,通過【】操作符得到的字符還可以被修改,要用到另外兩個重載的【】操作符:
- QCharRef operator[] (int position);
- QCharRef operator[] (uint position);
QCharRef operator[] (int position);
QCharRef operator[] (uint position);
返回的 QCharRef 類是一個輔助類,對它的修改將反映到原字符串中去。
統計
以下兩個成員函數都可以得到 QString 對象中字符的個數:
- int size() const;
- int length() const;
int size() const;
int length() const;
注意字符的個數並不一定等於字節數。