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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

QString

發布時間:2025/3/15 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 QString 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

QString

QString 存儲字符串釆用的是 Unicode 碼,每一個字符是一個 16 位的 QChar,而不是 8 位的 char,所以 QString 處理中文字符沒有問題,而且一個漢字算作是一個字符。

?

構造函數

  • QString(const QByteArray &ba)
  • QString(const char *str)
  • QString(QString &&other)
  • QString(const QString &other)
  • QString(QLatin1String str)
  • QString(int size, QChar ch)
  • QString(QChar ch)
  • QString(const QChar *unicode, int size = -1)
  • QString()

?

QString沒有提供直接從std::string或者其他平臺字符串類?的構造函數, 不過他提供豐富的靜態函數

  • QString?fromStdString(const std::string &str)
  • QString?fromStdU16String(const std::u16string &str)
  • QString?fromStdU32String(const std::u32string &str)
  • QString?fromStdWString(const std::wstring &str)
  • QString?fromUtf8(const char *str, int size = -1)
  • QString?fromUtf8(const QByteArray &str)
  • QString?fromUtf16(const ushort *unicode, int size = -1)
  • QString?fromUtf16(const char16_t *str, int size = -1)
  • QString?fromWCharArray(const wchar_t *string, int size = -1)

其他靜態函數

從數字轉字符串

  • QString?number(long n, int base = 10)
  • QString?number(int n, int base = 10)
  • QString?number(uint n, int base = 10)
  • QString?number(ulong n, int base = 10)
  • QString?number(qlonglong n, int base = 10)
  • QString?number(qulonglong n, int base = 10)
  • QString?number(double n, char format = 'g', int precision = 6) // format見下表,precision 為精度? ?
format解釋

e

格式化為 [-]9.9e[+|-]999

E

格式化為 [-]9.9E[+|-]999E

f

格式化為 [-]9.9

g

e 或者?f, 看那個更簡潔

GE 或者?f, 看那個更簡潔

?

?

?

?

?

?

常用函數

  • bool QString::isEmpty() const
  • bool QString::isNull() const

一個空字符串, isNull() 返回 false,而 isEmpty() 返回 true;只有未賦值的字符串,isNull() 才返回 true。

?

  • QString &append(const QString &str)
  • QString &prepend(const QString &str)

append() 在字符串的后面添加字符串,prepend() 在字符串的前面添加字符串, 同時提供了不同版本的重載

?

  • QString?arg(const QString &a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
  • QString QString::arg(double a, int fieldWidth = 0, char format = 'g', int precision = -1, QChar fillChar = QLatin1Char(' ')) const

字符串拼接,fieldWidth指定參數a占用的最小空間。 如果a所需的空間少于fieldWidth,則使用字符fillChar將其填充到fieldWidth。同樣改函數提供了多個版本的重載。

其中需要注意的為double版本的重載 , format解釋通上文number,??precision ?為精度

QString test("%1"); qDebug() << test.arg("hehe", 5, '*'); //"*hehe" qDebug() << test.arg("hehe", -5, '*'); //"hehe*" qDebug() << test.arg(3.51); //"3.51" qDebug() << test.arg(3.51, 5); //" 3.51" qDebug() << test.arg(3.51, 5, '0'); //"3.510000" qDebug() << test.arg(351.254634, 0, 'f', 3); //"351.255" qDebug() << test.arg(351.254634, 0, 'E', 3); //"3.513E+02" qDebug() << test.arg(351.254634, 0, 'G', 3); //"351"
  • int?count() const
  • int?count(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • int count(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
  • int count(const QStringRef &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;

無參count與size() 和 length()相同, 都返回字符串的字符個數,?字符串中如果有漢字,一個漢字算一個字符。

cs 表示是否區分大小寫,Qt :: CaseSensitive(默認),則搜索區分大小寫; Qt::CaseInsensitive不區分大小寫

QString ch = "123呵呵1234, xyZxyz"; qDebug() << ch.count(); //17 qDebug() << ch.count("123"); //2 qDebug() << ch.count("xyZ"); //1 qDebug() << ch.count("xyZ", Qt::CaseInsensitive); //2 qDebug() <<ch.count(QRegExp("\\d")); //7
  • QString QString::trimmed() const? ? //去掉字符串首尾的空格
  • QString QString::simplified() const //返回一個字符串,該字符串從開頭和結尾刪除了空格,并且內部空白序列都被一個空格替換。

QString str = " lots\t of\nwhitespace\r\n "; str = str.simplified(); // str == "lots of whitespace";
  • int QString::indexOf (const QString &str, int from = 0 , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • int QString::indexOf(QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

查找參數字符串 str /字符ch出現的位置,參數 from 是幵始查找的位置,Qt::CaseSensitivity cs 參數指定是否區分大小寫。

  • int QString::lastIndexOf(const QString &str, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • int QString::lastIndexOf(QChar ch, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

找某個字符串最后出現的位置。

QString x = "crazy azimuths"; QString y = "az"; x.lastIndexOf(y); // returns 6 x.lastIndexOf(y, 6); // returns 6 x.lastIndexOf(y, 5); // returns 2 x.lastIndexOf(y, 1); // returns -1
  • QString QString::left(int n) const //返回包含該字符串的最左邊的n個字符的字符串。如果n大于或等于size()或小于零,則返回整個字符串。
  • QString QString::right(int n) const //返回包含該字符串的最右邊的n個字符的字符串。如果n大于或等于size()或小于零,則返回整個字符串。
  • QString QString::mid(int position, int n = -1) const //返回從指定position位置開始,長度為n的字符串。如果position位置超過字符串的長度,則返回空字符串。 -1表示從指定位置開始可用的所有字符。
  • void QString::chop(int n)? ?//從QString末尾刪除n個字符。如果n大于或等于size(),則結果為空字符串。 如果n為負,則等于傳遞零。
  • QString QString::chopped(int len) const? ?//返回剪切chop的副本, 不改變原有QString
  • void QString::truncate(int position)? //從指定位置截斷字符串
QString x = "Pineapple", y; y = x.left(4); // y == "Pine" y = x.right(5); // y == "apple"QString x = "Nine pineapples"; QString y = x.mid(5, 4); // y == "pine" QString z = x.mid(5); // z == "pineapples"QString ch = "abcdefg"; qDebug() << ch.chopped(2);//"abcde" qDebug() << ch; //"abcdefg" ch.chop(2); qDebug() << ch; //"abcde"QString str = "Vladivostok"; str.truncate(4); // str == "Vlad"
  • bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const? ?//如果字符串以s結尾,則返回true;否則,返回true。 否則返回false。 cs同indexOf函數
  • bool QString::startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const //如果字符串以s開頭,則返回true;否則,返回true。 否則返回false。
QString str = "Bananas"; str.startsWith("Ban"); // returns true str.startsWith("Car"); // returns falsestr.endsWith("anas"); // returns true str.endsWith("pple"); // returns false
  • QString QString::section(QChar sep, int start, int end = ..., QString::SectionFlags flags = SectionDefault) const
  • QString QString::section(const QString &sep, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const
  • QString QString::section(const QRegExp &reg, int start, int end = -1, QString::SectionFlags flags = SectionDefault) const

從字符串中提取以 sep 作為分隔符,從 start 端到 end 端的字符串。字段從左開始編號為0、1、2等,?從右到左依次編號為-1,-2等

flags

SectionFlags描述

QString::SectionDefault

計算空字段,不包括前導和尾隨分隔符,并且區分大小寫比較分隔符。

QString::SectionSkipEmpty

將空字段視為不存在,即,就開始和結束而言,不考慮它們。

QString::SectionIncludeLeadingSep

在結果字符串中包括前導分隔符(如果有)。

QString::SectionIncludeTrailingSep

在結果字符串中包括結尾的分隔符(如果有)。

QString::SectionCaseInsensitiveSeps

不區分大小寫地比較分隔符。

?

?

?

?

?

QString str; QString csv = "forename,middlename,surname,phone"; QString path = "/usr/local/bin/myapp"; // First field is emptystr = csv.section(',', 2, 2); // str == "surname" str = path.section('/', 3, 4); // str == "bin/myapp" str = path.section('/', 3, 3, QString::SectionSkipEmpty); // str == "myapp"str = csv.section(',', -3, -2); // str == "middlename,surname" str = path.section('/', -1); // str == "myapp"QString str; QString data = "forename**middlename**surname**phone";str = data.section("**", 2, 2); // str == "surname" str = data.section("**", -3, -2); // str == "middlename**surname"QString line = "forename\tmiddlename surname \t \t phone"; QRegExp sep("\\s+"); str = line.section(sep, 2, 2); // str == "surname" str = line.section(sep, -3, -2); // str == "middlename surname"
  • QStringList QString::split(const QString &sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • QStringList?split(const QString &sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • QStringList?split(QChar sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • QStringList?split(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) const
  • QStringList?split(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const
  • QVector<QStringRef>?splitRef(const QString &sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • QVector<QStringRef>?splitRef(QChar sep, QString::SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
  • QVector<QStringRef>?splitRef(const QRegExp &rx, QString::SplitBehavior behavior = KeepEmptyParts) const
  • QVector<QStringRef>?splitRef(const QRegularExpression &re, QString::SplitBehavior behavior = KeepEmptyParts) const

拆分字符串

str = "/a/b/c/"; auto parts = str.split('/'); // parts: {"", "a", "b", "c", ""}QString str; QStringList list;str = "Some text\n\twith strange whitespace."; list = str.split(QRegExp("\\s+"));//list:[ "Some","text","with","strange","whitespace." ]

?

總結

以上是生活随笔為你收集整理的QString的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。