<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Qt5 中的字符串 > 原文: [http://zetcode.com/gui/qt5/strings/](http://zetcode.com/gui/qt5/strings/) 在本章中,我們將使用字符串。 Qt5 具有用于處理字符串的`QString`類。 它非常強大,并且具有多種方法。 `QString`類提供 Unicode 字符串。 它將字符串存儲為 16 位`QChars`。 每個`QChar`對應一個 Unicode 4.0 字符。 與許多其他編程語言中的字符串不同,可以修改`QString`。 在本章的示例中,我們將不需要 Qt GUI 模塊。 我們將創建命令行程序。 由于默認情況下包括 Qt GUI,因此我們可以通過在項目文件中添加`QT -= gui`聲明來禁用它。 ## 第一個例子 在第一個示例中,我們將使用`QString`類的一些基本方法。 `basic.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString a = "love"; a.append(" chess"); a.prepend("I "); out << a << endl; out << "The a string has " << a.count() << " characters" << endl; out << a.toUpper() << endl; out << a.toLower() << endl; return 0; } ``` 在代碼示例中,我們啟動`QString`。 我們附加并添加一些其他文字。 我們打印字符串的長度。 最后,我們以大寫和小寫形式顯示修改后的字符串。 ```cpp QString a = "love"; ``` 啟動`QString`。 ```cpp a.append(" chess"); a.prepend("I "); ``` 我們將文本附加和添加到初始字符串之前。 該字符串就地修改。 ```cpp out << a << endl; ``` 終端上印有`I love chess`。 ```cpp out << "The a string has " << a.count() << " characters" << endl; ``` `count()`方法返回字符串中的字符數。 `length()`和`size()`方法是等效的。 ```cpp out << a.toUpper() << endl; out << a.toLower() << endl; ``` 這兩個方法返回字符串的大寫和小寫副本。 他們不修改字符串,而是返回新的修改后的字符串副本。 輸出: ```cpp $ ./basic I love chess The a string has 12 characters I LOVE CHESS i love chess ``` ## 初始化字符串 `QString`可以通過多種方式初始化。 `init.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString str1 = "The night train"; out << str1 << endl; QString str2("A yellow rose"); out << str2 << endl; std::string s1 = "A blue sky"; QString str3 = s1.c_str(); out << str3 << endl; std::string s2 = "A thick fog"; QString str4 = QString::fromLatin1(s2.data(), s2.size()); out << str4 << endl; char s3[] = "A deep forest"; QString str5(s3); out << str5 << endl; return 0; } ``` 我們介紹了五種初始化`QString`的方法。 ```cpp QString str1 = "The night train"; ``` 這是在計算機語言中初始化字符串的傳統方式。 ```cpp QString str2("A yellow rose"); ``` 這是啟動`QString`的對象方式。 ```cpp std::string s1 = "A blue sky"; QString str3 = s1.c_str(); ``` 我們有一個來自 C++ 標準庫的字符串對象。 我們使用其`c_str()`方法來生成以空字符結尾的字符序列。 該字符數組(字符串的經典 C 表示形式)可以分配給`QString`變量。 ```cpp std::string s2 = "A thick fog"; QString str4 = QString::fromLatin1(s2.data(), s2.size()); ``` 在這些代碼行中,我們將標準 C++ 字符串轉換為`QString`。 我們利用`fromLatin1()`方法。 它需要一個指向字符數組的指針。 指針通過`std::string`的`data()`方法返回。 第二個參數是`std::string`的大小。 ```cpp char s3[] = "A deep forest"; QString str5(s3); ``` 這是一個 C 字符串; 它是一個字符數組。 `QString`構造器之一可以將`char`數組作為參數。 輸出: ```cpp $ ./init The night train A yellow rose A blue sky A thick fog A deep forest ``` ## 訪問字符串元素 `QString`是`QChars`的序列。 可以使用`[]`運算符或`at()`方法訪問字符串的元素。 `access.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString a = "Eagle"; out << a[0] << endl; out << a[4] << endl; out << a.at(0) << endl; if (a.at(5).isNull()) { out << "Outside the range of the string" << endl; } return 0; } ``` 我們從特定的`QString`打印一些單獨的字符。 ```cpp out << a[0] << endl; out << a[4] << endl; ``` 我們打印字符串的第一個和第五個元素。 ```cpp out << a.at(0) << endl; ``` 使用`at()`方法,我們檢索字符串的第一個字符。 ```cpp if (a.at(5).isNull()) { out << "Outside the range of the string" << endl; } ``` 如果我們嘗試訪問字符串字符范圍之外的字符,則`at()`方法返回`null`。 輸出: ```cpp $ ./access E e E Outside the range of the string ``` ## 字符串長度 有三種獲取字符串長度的方法。 `size()`,`count()`和`length()`方法。 所有人都做同樣的事情。 它們返回指定字符串中的字符數。 `length.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString s1 = "Eagle"; QString s2 = "Eagle\n"; QString s3 = "Eagle "; QString s4 = "орел"; out << s1.length() << endl; out << s2.length() << endl; out << s3.length() << endl; out << s4.length() << endl; return 0; } ``` 我們得到四個字符串的大小。 ```cpp QString s2 = "Eagle\n"; QString s3 = "Eagle "; ``` 這兩個字符串中的每個字符串都有一個白色字符。 ```cpp QString s4 = "орел"; ``` 此字符串由俄語字母組成。 輸出: ```cpp $ ./length 5 6 6 4 ``` 從輸出中我們可以看到`length()`方法也計算了白色字符。 ## 字符串構建 動態字符串構建使我們可以用實際值替換特定的控制字符。 我們使用`arg()`方法進行插值。 `building.cpp` ```cpp #include <QTextStream> int main() { QTextStream out(stdout); QString s1 = "There are %1 white roses"; int n = 12; out << s1.arg(n) << endl; QString s2 = "The tree is %1 m high"; double h = 5.65; out << s2.arg(h) << endl; QString s3 = "We have %1 lemons and %2 oranges"; int ln = 12; int on = 4; out << s3.arg(ln).arg(on) << endl; return 0; } ``` 將要替換的標記以`%`字符開頭。 下一個字符是指定自變量的數字。 一個字符串可以有多個參數。 `arg()`方法已重載,它可以接受整數,長數字,字符和`QChar`等。 ```cpp QString s1 = "There are %1 white roses"; int n = 12; ``` `%1`是我們計劃替換的標記。 我們定義了一個整數。 ```cpp out << s1.arg(n) << endl; ``` `arg()`方法采用整數。 `%1`標記被`n`變量的值替換。 ```cpp QString s2 = "The tree is %1 m high"; double h = 5.65; out << s2.arg(h) << endl; ``` 這三行對于一個雙數執行相同的操作。 正確的`arg()`方法將被自動調用。 ```cpp QString s3 = "We have %1 lemons and %2 oranges"; int ln = 12; int on = 4; out << s3.arg(ln).arg(on) << endl; ``` 我們可以有多個控制字符。 `%1`引用第一個參數,`%2`引用第二個參數。 `arg()`方法在連續的鏈中調用。 輸出: ```cpp $ ./building There are 12 white roses The tree is 5.65 m high We have 12 lemons and 4 oranges ``` ## 子串 在進行文本處理時,我們需要找到普通字符串的子字符串。 我們有`left()`,`mid()`和`right()`方法可供我們使用。 `substrings.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString str = "The night train"; out << str.right(5) << endl; out << str.left(9) << endl; out << str.mid(4, 5) << endl; QString str2("The big apple"); QStringRef sub(&str2, 0, 7); out << sub.toString() << endl; return 0; } ``` 我們將使用這三種方法查找給定字符串的一些子字符串。 ```cpp out << str.right(5) << endl; ``` 通過`right()`方法,我們獲得`str`字符串的最右邊五個字符。 將打印`train`。 ```cpp out << str.left(9) << endl; ``` 使用`left()`方法,我們獲得`str`字符串的最左邊九個字符。 將打印`night`。 ```cpp out << str.mid(4, 5) << endl; ``` 使用`mid()`方法,我們從第 4 個位置開始得到 5 個字符。 打印`night`。 ```cpp QString str2("The big apple"); QStringRef sub(&str2, 0, 7); ``` `QStringRef`類是`QString`的只讀版本。 在這里,我們創建`str2`字符串一部分的`QStringRef`。 第二個參數是位置,第三個參數是子字符串的長度。 輸出: ```cpp $ ./substrings train The night night The big ``` ## 遍歷字符串 `QString`由`QChars`組成。 我們可以遍歷`QString`以訪問字符串的每個元素。 `looping.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString str = "There are many stars."; foreach (QChar qc, str) { out << qc << " "; } out << endl; for (QChar *it=str.begin(); it!=str.end(); ++it) { out << *it << " " ; } out << endl; for (int i = 0; i < str.size(); ++i) { out << str.at(i) << " "; } out << endl; return 0; } ``` 我們展示了通過`QString`的三種方法。 在將字母打印到終端時,我們在字母之間添加空格字符。 ```cpp foreach (QChar qc, str) { out << qc << " "; } ``` `foreach`關鍵字是 C++ 語言的 Qt 擴展。 關鍵字的第一個參數是字符串元素,第二個參數是字符串。 ```cpp for (QChar *it=str.begin(); it!=str.end(); ++it) { out << *it << " " ; } ``` 在此代碼中,我們使用迭代器遍歷字符串。 ```cpp for (int i = 0; i < str.size(); ++i) { out << str.at(i) << " "; } ``` 我們計算字符串的大小,并使用`at()`方法訪問字符串元素。 輸出: ```cpp $ ./looping T h e r e a r e m a n y s t a r s . T h e r e a r e m a n y s t a r s . T h e r e a r e m a n y s t a r s . ``` ## 字符串比較 `QString::compare()`靜態方法用于比較兩個字符串。 該方法返回一個整數。 如果返回的值小于零,則第一個字符串小于第二個字符串。 如果返回零,則兩個字符串相等。 最后,如果返回的值大于零,則第一個字符串大于第二個字符串。 “較少”是指字符串的特定字符在字符表中位于另一個字符之前。 比較字符串的方法如下:比較兩個字符串的第一個字符; 如果它們相等,則比較以下兩個字符,直到找到一些不同的字符或發現所有字符都匹配為止。 `comparing.cpp` ```cpp #include <QTextStream> #define STR_EQUAL 0 int main(void) { QTextStream out(stdout); QString a = "Rain"; QString b = "rain"; QString c = "rain\n"; if (QString::compare(a, b) == STR_EQUAL) { out << "a, b are equal" << endl; } else { out << "a, b are not equal" << endl; } out << "In case insensitive comparison:" << endl; if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) { out << "a, b are equal" << endl; } else { out << "a, b are not equal" << endl; } if (QString::compare(b, c) == STR_EQUAL) { out << "b, c are equal" << endl; } else { out << "b, c are not equal" << endl; } c.chop(1); out << "After removing the new line character" << endl; if (QString::compare(b, c) == STR_EQUAL) { out << "b, c are equal" << endl; } else { out << "b, c are not equal" << endl; } return 0; } ``` 我們將使用`compare()`方法進行區分大小寫和不區分大小寫的比較。 ```cpp #define STR_EQUAL 0 ``` 為了更好的代碼清晰度,我們定義`STR_EQUAL`常量。 ```cpp QString a = "Rain"; QString b = "rain"; QString c = "rain\n"; ``` 我們將比較這三個字符串。 ```cpp if (QString::compare(a, b) == STR_EQUAL) { out << "a, b are equal" << endl; } else { out << "a, b are not equal" << endl; } ``` 我們比較`a`和`b`字符串,它們不相等。 它們的第一個字符不同。 ```cpp if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) { out << "a, b are equal" << endl; } else { out << "a, b are not equal" << endl; } ``` 如果不區分大小寫,則字符串相等。 `Qt::CaseInsensitive`使比較大小寫不敏感。 ```cpp c.chop(1); ``` `chop()`方法從`c`字符串中刪除最后一個字符。 現在`b`和`c`字符串相等。 輸出: ```cpp $ ./comparing a, b are not equal In case insensitive comparison: a, b are equal b, c are not equal After removing the new line character b, c are equal ``` ## 轉換字符串 字符串通常需要轉換為其他數據類型,反之亦然。 `toInt()`,`toFloat()`和`toLong()`是將字符串轉換為整數,浮點數和長整數的三種`QString`方法。 (還有更多這樣的方法。)`setNum()`方法將各種數字數據類型轉換為字符串。 該方法已重載,并且自動調用了正確的方法。 `Output` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString s1 = "12"; QString s2 = "15"; QString s3, s4; out << s1.toInt() + s2.toInt() << endl; int n1 = 30; int n2 = 40; out << s3.setNum(n1) + s4.setNum(n2) << endl; return 0; } ``` 在示例中,我們將兩個字符串轉換為整數并將其相加。 然后,我們將兩個整數轉換為字符串并將其連接起來。 ```cpp out << s1.toInt() + s2.toInt() << endl; ``` `toInt()`方法將字符串轉換為整數。 我們添加兩個轉換為字符串的數字。 ```cpp out << s3.setNum(n1) + s4.setNum(n2) << endl; ``` 在這種情況下,`setNum()`方法將整數轉換為字符串。 我們連接兩個字符串。 輸出: ```cpp $ ./converts 27 3040 ``` ## 字符 字符分為各種類別:數字,字母,空格和標點符號。 每個`QString`都由`QChar`組成。 `QChar`具有`isDigit()`,`isLetter()`,`isSpace()`和`isPunct()`方法來執行作業。 `letters.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); int digits = 0; int letters = 0; int spaces = 0; int puncts = 0; QString str = "7 white, 3 red roses."; foreach(QChar s, str) { if (s.isDigit()) { digits++; } else if (s.isLetter()) { letters++; } else if (s.isSpace()) { spaces++; } else if (s.isPunct()) { puncts++; } } out << QString("There are %1 characters").arg(str.count()) << endl; out << QString("There are %1 letters").arg(letters) << endl; out << QString("There are %1 digits").arg(digits) << endl; out << QString("There are %1 spaces").arg(spaces) << endl; out << QString("There are %1 punctuation characters").arg(puncts) << endl; return 0; } ``` 在示例中,我們定義了一個簡單的句子。 我們將計算句子中的數字,字母,空格和標點符號的數量。 ```cpp int digits = 0; int letters = 0; int spaces = 0; int puncts = 0; ``` 我們為每個字符類別定義一個整數變量。 ```cpp QString str = "7 white, 3 red roses."; ``` 這是要檢查的句子。 ```cpp foreach(QChar s, str) { if (s.isDigit()) { digits++; } else if (s.isLetter()) { letters++; } else if (s.isSpace()) { spaces++; } else if (s.isPunct()) { puncts++; } } ``` 我們使用`foreach`關鍵字來瀏覽`QString`。 每個元素都是`QChar`。 我們使用`QChar`類的方法來確定字符的類別。 ```cpp out << QString("There are %1 characters").arg(str.count()) << endl; out << QString("There are %1 letters").arg(letters) << endl; out << QString("There are %1 digits").arg(digits) << endl; out << QString("There are %1 spaces").arg(spaces) << endl; out << QString("There are %1 punctuation characters").arg(puncts) << endl; ``` 使用字符串插值,我們將數字打印到終端。 輸出: ```cpp $ ./letters There are 21 characters There are 13 letters There are 2 digits There are 4 spaces There are 2 punctuation characters ``` ## 修改字符串 某些方法(例如`toLower()`方法)返回原始字符串的新修改副本。 其他方法可就地修改字符串。 我們將介紹其中的一些。 `modify.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString str = "Lovely"; str.append(" season"); out << str << endl; str.remove(10, 3); out << str << endl; str.replace(7, 3, "girl"); out << str << endl; str.clear(); if (str.isEmpty()) { out << "The string is empty" << endl; } return 0; } ``` 我們描述了四種就地修改字符串的方法。 ```cpp str.append(" season"); ``` `append()`方法在字符串的末尾添加一個新字符串。 ```cpp str.remove(10, 3); ``` `remove()`方法從位置 10 開始從字符串中刪除 3 個字符。 ```cpp str.replace(7, 3, "girl"); ``` `replace()`方法用指定的字符串替換從`7`位置開始的`3`字符。 ```cpp str.clear(); ``` `clear()`方法清除字符串。 輸出: ```cpp $ ./modify Lovely season Lovely sea Lovely girl The string is empty ``` 在本章中,我們使用了 Qt5 中的字符串。 ## 對齊字符串 擁有整潔的輸出是常見的要求。 我們可以使用`leftJustified()`和`rightJustified()`方法來對齊字符串。 `right_align.cpp` ```cpp #include <QTextStream> int main(void) { QTextStream out(stdout); QString field1 = "Name: "; QString field2 = "Occupation: "; QString field3 = "Residence: "; QString field4 = "Marital status: "; int width = field4.size(); out << field1.rightJustified(width, ' ') << "Robert\n"; out << field2.rightJustified(width, ' ') << "programmer\n"; out << field3.rightJustified(width, ' ') << "New York\n"; out << field4.rightJustified(width, ' ') << "single\n"; return 0; } ``` 該示例將字段字符串向右對齊。 ```cpp int width = field4.size(); ``` 我們計算最寬字符串的大小。 ```cpp out << field1.rightJustified(width, ' ') << "Robert\n"; ``` `rightJustified()`方法返回具有`width`字符的字符串。 如果字符串較短,則其余部分將使用提供的字符填充。 在我們的情況下,它是一個空格字符。 輸出: ```cpp $ ./right_align Name: Robert Occupation: programmer Residence: New York Marital status: single ``` ## 轉義字符 Qt5 具有`toHtmlEscaped()`方法,該方法將純文本字符串轉換為將 HTML 元字符`<`,`>`,`&`和`"`替換為 HTML 命名實體的 HTML 字符串。 ```cpp $ cat cprog.c #include <stdio.h> int main(void) { for (int i=1; i<=10; i++) { printf("Bottle %d\n", i); } } ``` 此 C 包含 HTML 元字符。 `html_escape.cpp` ```cpp #include <QTextStream> #include <QFile> int main(void) { QTextStream out(stdout); QFile file("cprog.c"); if (!file.open(QIODevice::ReadOnly)) { qWarning("Cannot open file for reading"); return 1; } QTextStream in(&file); QString allText = in.readAll(); out << allText.toHtmlEscaped() << endl; file.close(); return 0; } ``` 該示例讀取一個 C 程序,并將元字符替換為其命名的實體。 輸出: ```cpp $ ./html_escape #include <stdio.h> int main(void) { for (int i=1; i<=10; i++) { printf(&quot;Bottle %d\n&quot;, i); } } ``` 在本章中,我們使用了 Qt5 中的字符串。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看