qt QString常用函数simplified replace remove split indexOf mid left right prepend repeated number arg

2023-11-12

以下是QString常用函数使用过程中的总结,例子大多数摘抄自qt自带的帮助文档

1、simplified

simplified()功能,这个函数把一个字符串首尾的空格全部清除,不管首尾是几个空格哦。字符串中间的空格(包括单个空格、多个空格、\t、\n)都统一转化成一个空格,这样就方便提取了,我们再使用split()函数就能很好拆分了。

qt帮助文档介绍.

QString QString::simplified() const

Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.

Whitespace means any character for which QChar::isSpace() returns true. This includes the ASCII characters '\t', '\n', '\v', '\f', '\r', and ' '.

Example:

  QString str = "  lots\t of\nwhitespace\r\n ";
  str = str.simplified();
  // str == "lots of whitespace";

See also trimmed().

 

2、replace

字符替换,replace有很大重载函数这里只摘抄一个。

qt帮助文档介绍.

QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This function overloads replace().

Replaces every occurrence of the string before with the string after and returns a reference to this string.

If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.

Example:

  QString str = "colour behaviour flavour neighbour";
  str.replace(QString("ou"), QString("o"));
  // str == "color behavior flavor neighbor"
Note: The replacement text is not rescanned after it is inserted.

Example:

  QString equis = "xxxxxx";
  equis.replace("xx", "x");
  // equis == "xxx"

3、remove

字符串移除,remove同样的也有很大重载函数,这里只说有一个,就是移除从第position开始的n个字符

qt帮助文档介绍.

QString &QString::remove(int position, int n)

Removes n characters from the string, starting at the given position index, and returns a reference to the string.

If the specified position index is within the string, but position + n is beyond the end of the string, the string is truncated at the specified position.

  QString s = "Montreal";
  s.remove(1, 4);
  // s == "Meal"

See also insert() and replace().

4、split

字符串分割,remove同样的也有很大重载函数,这里只摘抄一个

qt帮助文档介绍.

QStringList QString::split(const QString &sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Splits the string into substrings wherever sep occurs, and returns the list of those strings. If sep does not match anywhere in the string, split() returns a single-element list containing this string.

cs specifies whether sep should be matched case sensitively or case insensitively.

If behavior is QString::SkipEmptyParts, empty entries don't appear in the result. By default, empty entries are kept.

Example:

  QString str = "a,,b,c";

  QStringList list1 = str.split(',');
  // list1: [ "a", "", "b", "c" ]

  QStringList list2 = str.split(',', QString::SkipEmptyParts);
  // list2: [ "a", "b", "c" ]
See also QStringList::join() and section().

5、indexOf

字符串查找/截取,indexOf同样的也有很大重载函数,这里只摘抄一个,查找到第一个匹配的字符串,如果找不到返回-1.

qt帮助文档介绍.

int QString::indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns the index position of the first occurrence of the string str in this string, searching forward from index position from. Returns -1 if str is not found.

If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.

Example:

  QString x = "sticky question";
  QString y = "sti";
  x.indexOf(y);               // returns 0
  x.indexOf(y, 1);            // returns 10
  x.indexOf(y, 10);           // returns 10
  x.indexOf(y, 11);           // returns -1

6、mid

截取从n个字符串,重position开始截取,截取n个字符串,如果n默认为-1,为-1代表从position开始截取,截取到字符串结束。

QString QString::mid(int position, int n = -1) const

Returns a string that contains n characters of this string, starting at the specified position index.

Returns a null string if the position index exceeds the length of the string. If there are less than n characters available in the string starting at the given position, or if n is -1 (default), the function returns all characters that are available from the specified position.

Example:

  QString x = "Nine pineapples";
  QString y = x.mid(5, 4);            // y == "pine"
  QString z = x.mid(5);               // z == "pineapples"

7、left

从左边开始截取n个字符串

QString QString::left(int n) const

Returns a substring that contains the n leftmost characters of the string.

The entire string is returned if n is greater than or equal to size(), or less than zero.

  QString x = "Pineapple";
  QString y = x.left(4);      // y == "Pine"

8、right

从右边开始截取n个字符串

QString QString::right(int n) const

Returns a substring that contains the n rightmost characters of the string.

The entire string is returned if n is greater than or equal to size(), or less than zero.


  QString x = "Pineapple";
  QString y = x.right(5);      // y == "apple"

8、prepend

在头部追加字符串

QString &QString::prepend(const QString &str)

Prepends the string str to the beginning of this string and returns a reference to this string.

Example:

  QString x = "ship";
  QString y = "air";
  x.prepend(y);
  // x == "airship"

9、repeated

重复字符串

QString QString::repeated(int times) const

Returns a copy of this string repeated the specified number of times.

If times is less than 1, an empty string is returned.

Example:

  QString str("ab");
  str.repeated(4);            // returns "abababab"

10、number

多重载函数,字符串和数值之间的转换,可以是10进制或者16进制。

[static] QString QString::number(long n, int base = 10)

Returns a string equivalent of the number n according to the specified base.

The base is 10 by default and must be between 2 and 36. For bases other than 10, n is treated as an unsigned integer.

The formatting always uses QLocale::C, i.e., English/UnitedStates. To get a localized string representation of a number, use QLocale::toString() with the appropriate locale.

  long a = 63;
  QString s = QString::number(a, 16);             // s == "3f"
  QString t = QString::number(a, 16).toUpper();     // t == "3F"
   
  QString r = QString::number(a,'f',2);            //2代表保留2位小数,63.00 
  QString u = QString::number(a).sprintf("%2d",a)  //d代表整数,2代表两位,sprintf和printf有点类似,a格式化成两位整数(不会自动补零)

    
   strNumber = strNumber.sprintf("%02d:%02d:%02d", hour, minute, seconds);//%02d:0代表补位的值为零、2表示位数。

11、sprintf

格式化输出

QString str;
str.sprintf("%s","Welcome ");     //str = "Welcome "
str.sprintf("%s"," to you! ");      //str = " to you! "
str.sprintf("%s %s","Welcome "," to you! ");     //str = "Welcome  to you! ";
QString u = QString::number(a).sprintf("%2d",a)  //d代表整数,2代表两位,sprintf和printf有点类似,a格式化成两位整数(不会自动补零)
strNumber = strNumber.sprintf("%02d:%02d:%02d", hour, minute, seconds);//%02d:0代表补位的值为零、2表示位数。

12、arg

arg的重载函数也特别多,

QString QString::arg(long a, int fieldWidth = 0, int base = 10, QChar fillChar = QLatin1Char( ' ' )) const

This function overloads arg().

fieldWidth specifies the minimum amount of space that a is padded to and filled with the character fillChar. A positive value produces right-aligned text; a negative value produces left-aligned text.

The a argument is expressed in the given base, which is 10 by default and must be between 2 and 36.

The '%' can be followed by an 'L', in which case the sequence is replaced with a localized representation of a. The conversion uses the default locale. The default locale is determined from the system's locale settings at application startup. It can be changed using QLocale::setDefault(). The 'L' flag is ignored if base is not 10.

  QString str;
  str = QString("Decimal 63 is %1 in hexadecimal")
          .arg(63, 0, 16);
  // str == "Decimal 63 is 3f in hexadecimal"

  QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
  str = QString("%1 %L2 %L3")
          .arg(12345)
          .arg(12345)
          .arg(12345, 0, 16);
  // str == "12345 12,345 3039"

If fillChar is '0' (the number 0, ASCII 48), the locale's zero is used. For negative numbers, zero padding might appear before the minus sign.

 

不足补零

int num = 1;

QString("%1").arg(num,2, 10, QChar('0'));

这样输出的就是01.

其中2代表要输出几位,10代表10进制转换,QChar('0')代表不足补0(可以更换成你自己想要的字母)

还有好多函数,今天就先写到这吧

另外还可以参考:

https://blog.csdn.net/asd1147170607/article/details/105630837

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

qt QString常用函数simplified replace remove split indexOf mid left right prepend repeated number arg 的相关文章

  • Qt程序部署到多平台,如何?

    我是 Qt 编程新手 我想开发一个程序 我想在 Windows Linux ubuntu 和 Mac 上运行 听说Qt支持多平台应用程序开发 但我的问题是 在我部署或编译后 任何 Qt 库都需要在 Ubuntu 中运行这个应用程序吗 如果您
  • 如何在 C++ 运行时更改 QML 对象的属性?

    我想在运行时更改 QML 对象的文本 我尝试如下 但文本仍然为空 这是后端类 class BackEnd public QObject Q OBJECT Q PROPERTY QString userFieldText READ userF
  • 当我尝试构建 Qt 4.7.1 静态库时,“找不到 -ljscore”

    我尝试从最新的源构建静态 Qt 库 但出现以下错误 usr bin ld cannot find ljscore collect2 ld returned 1 exit status 如何解决这个问题呢 这是 Qt 构建系统中自 4 7 0
  • Qt中如何获取鼠标在屏幕上的位置?

    我想获取屏幕上的鼠标坐标 我怎样才能在 Qt 中做到这一点 在 Windows 上 使用 C 我正在做类似答案中建议的事情对于这个问题 https stackoverflow com q 11737665 1420197 正如文档所述 QC
  • Qt WinRT 应用程序无法访问文件权限被拒绝

    我需要使用 Qt 和 FFMPEG 开发 WinRT 应用程序 我根据指令构建了 WinRT 的 ffmpeghere https github com Microsoft FFmpegInterop我可以将库与我的项目链接起来 现在我需要
  • QTextEdit.find() 在 Python 中不起作用

    演示问题的简单代码 usr bin env python import sys from PyQt4 QtCore import QObject SIGNAL from PyQt4 QtGui import QApplication QTe
  • 如何在带有预编译头的项目中使用google protobuf

    我有一个包含多个项目的解决方案 我的项目 但不是全部 使用预编译头 我决定使用 protobuf 但遇到了一个问题 在 protoc exe 从 proto 生成 pb h 后 我尝试包含标头并收到错误 预编译标头未包含在 pb h 中 我
  • 如何使用 Qtimer 添加 1 秒延迟

    我目前有一个方法如下 void SomeMethod int a Delay for one sec timer gt start 1000 After one sec SomeOtherFunction a 这个方法实际上是一个附加到信号
  • 向 Qt 样式表添加特异性时丢失样式

    这是我的代码 const QString STYLE SHEET background color rgba x x x y border 1px solid gray border radius 0px border top 1px so
  • 如何创建用于 QML 的通用对象模型?

    我想知道是否有任何宏或方法如何将 Qt 模型注册为 QObject 的属性 例如 我有AnimalModel http doc qt io qt 5 qtquick modelviewsdata cppmodels html qabstra
  • QCombobox 向下箭头图像

    如何更改Qcombobox向下箭头图像 现在我正在使用这个 QSS 代码 但这不起作用 我无法删除向下箭头边框 QComboBox border 0px QComboBox down arrow border 0px background
  • 更改 Qt OpenGL 窗口示例以使用 OpenGL 3.3

    我正在尝试更改 Qt OpenGL 示例以使用更现代的 opengl 版本 330 似乎合适 所以我做了 在 main cpp 上设置版本和配置文件 设置着色器版本 更改着色器以使用统一 它现在构建没有任何错误 但我只看到一个空白窗口 我错
  • 与 Qt 项目的静态链接

    我有一个在 Visual Studio 2010 Professional 中构建的 Qt 项目 但是 当我运行它 在调试或发布模式下 时 它会要求一些 Qt dll 如果我提供 dll 并将它们放入 System32 中 它就可以工作 但
  • 在 MacOS 终端上运行 ffmpeg [关闭]

    Closed 这个问题是无关 help closed questions 目前不接受答案 我对 MacOS 相当陌生 我发现使用终端来获取信息并不容易ffmpeg和我在 Window 上一样正常运行 我有 ffmpeg 二进制文件ffmpe
  • Qt 5.6 测试版 Visual Studio 2015

    我已经安装了这个 http download qt io development releases qt 5 6 5 6 0 beta qt opensource windows x86 msvc2015 5 6 0 beta exe mi
  • Qt - 无法让 lambda 工作[重复]

    这个问题在这里已经有答案了 我有以下功能 我想在其中修剪我的std set
  • Qt - 设置不可编辑的QComboBox的显示文本

    我想将 QComboBox 的文本设置为某些自定义文本 不在 QComboBox 的列表中 而不将此文本添加为 QComboBox 的项目 此行为可以在可编辑的 QComboBox 上实现QComboBox setEditText cons
  • 在高 dpi Windows 平台上自动重新缩放应用程序?

    我正在编写一个需要在高 dpi Windows 192dpi 而不是 96dpi 上运行的 Qt 应用程序 不幸的是 Qt 框架尚不支持高 dpi 至少在 Windows 上 因此我的应用程序及其所有元素看起来只有应有尺寸的一半 有没有办法
  • 如何为 Windows 构建静态 Qt 库并将其与 Qt Creator 一起使用

    我已经下载了以下 Qt 源 http download qt nokia com qt source qt everywhere opensource src 4 7 3 zip http download qt nokia com qt
  • 将 QByteArray 从大端转换为小端

    我想我在这里有点不知所措 我尝试了这么简单的事情 我不敢相信没有任何内置的 Qt 使用 Qt 5 6 2 我尝试将 QByteArray 内的数据从大端转换为小端 总是从相同的测试 QByteArray 开始 就像这样 QByteArray

随机推荐