当前位置: 首页 > news >正文

QString类方法和变量简介(全)

QString类方法和变量简介

    • 操作字符串(+=|append|insert|sprintf|QString::arg()|prepend|replace|trimmed|simplified)
    • 查询字符串(startsWith|endsWith|contains|localeAwareCompare|compare)
    • 字符串转换


标准C++提供了两种字符串:一种是C语言风格的以"\0"字符结尾的字符数组;另一类是字符串类String。QT字符串类QString的功能更强大。QString类保存16位Unicode值。

操作字符串(+=|append|insert|sprintf|QString::arg()|prepend|replace|trimmed|simplified)

(1)QString提供一个二元的"+"操作符用于组合两个字符串,提供一个"+="操作符用于追加一个字符串到另一个字符串的末尾。

QString str1 = "Welcome ";
str1 = str1 + "to you!";
QString str2 = "Hello ";
str2 += "World!";

QString str1 = "Welcome " 将一个const char*类型的ASCII字符串 "Welcome"传递个QString,它被解释为一个典型的以"\0"结尾的C类型的字符串,这就导致会调用QString的构造函数来初始化字符串。

(2)QString::append()函数具有同"+="操作符相同的功能,在一个字符串的末尾追加另一个字符串

QString str1 = "Welcome ";
QString str2 = "to ";
str1.append(str2);
str1.append("you!");
// QT助手
QString &QString::append(const QString &str)
Appends the string str onto the end of this string.
Example:QString x = "free";QString y = "dom";x.append(y);// x == "freedom"
This is the same as using the insert() function:x.insert(x.size(), y);
The append() function is typically very fast (constant time), because QString preallocates extra space at the end of the string data so it can grow without reallocating the entire string each time.

(3)QString::sprintf()函数可用于组合字符串,其支持的格式定义符同C++库中的函数sprintf()定义一样。

QString str;
str.sprintf("%s","Welcome ");
str.sprintf("%s","to you!");
str.sprintf("%s %s","Welcome ","to you!");
// QT助手
QString &QString::sprintf(const char *cformat, ...)
This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
Use asprintf(), arg() or QTextStream instead.

(4)QString::arg()函数是QT提供的另一种方便的字符串组合方式。相对于QString::sprintf()函数,QString::arg()函数是类型安全,完全支持Unicode,并且允许改变"%n"参数的顺序。

QString str;
str = QString("%1 is %2 years old.").arg("Xiaoming").arg(24); // Xiaoming is 24 years old
// QT助手
QString QString::arg(const QString &a, int fieldWidth = 0, QChar fillChar = QLatin1Char(' ')) const
Returns a copy of this string with the lowest numbered place marker replaced by string a, i.e., %1, %2, ..., %99.
fieldWidth specifies the minimum amount of space that argument a shall occupy. If a requires less space than fieldWidth, it is padded to fieldWidth with character fillChar. A positive fieldWidth produces right-aligned text. A negative fieldWidth produces left-aligned text.
This example shows how we might create a status string for reporting progress while processing a list of files:QString i;           // current file's numberQString total;       // number of files to processQString fileName;    // current file's nameQString status = QString("Processing file %1 of %2: %3").arg(i).arg(total).arg(fileName);
First, arg(i) replaces %1. Then arg(total) replaces %2. Finally, arg(fileName) replaces %3.
One advantage of using arg() over asprintf() is that the order of the numbered place markers can change, if the application's strings are translated into other languages, but each arg() will still replace the lowest numbered unreplaced place marker, no matter where it appears. Also, if place marker %i appears more than once in the string, the arg() replaces all of them.
If there is no unreplaced place marker remaining, a warning message is output and the result is undefined. Place marker numbers must be in the range 1 to 99.

(5)其他字符串组合方法

  • insert()函数: 在原字符串特定的位置插入另一个字符串。
  • prepend()函数:在原字符串的开头插入另一个字符串。
  • replace()函数:用指定的字符串代替原字符串中的某些字符

(6)移除字符串两端的空白(空白字符包括回车符"\n"、换行字符"\r"、制表符"\t"、空格字符" "等)

  • QString::trimmed()函数:移除字符串两端的空白字符
// QT助手
QString QString::trimmed() const
Returns a string that has whitespace removed from the start and the end.
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.trimmed();// str == "lots\t of\nwhitespace"
Unlike simplified(), trimmed() leaves internal whitespace alone.
  • QString::simplified()函数:移除字符串两端的空白字符,使用单个空格字符" "代替出现的空白字符
// 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";

查询字符串(startsWith|endsWith|contains|localeAwareCompare|compare)

(1)QString::startsWith()函数判断一个字符串是否以某个字符串开头。第一个参数指定一个字符串,第二个参数指定是否大小写敏感(默认大小写敏感)

    QString str = "Welcome to you! ";str.startsWith("Welcome", Qt::CaseSensitive); // 返回 truestr.startsWith("you", Qt::CaseSensitive); // 返回 false
// QT助手
bool QString::startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.QString str = "Bananas";str.startsWith("Ban");     // returns truestr.startsWith("Car");     // returns false

(2)QString::endsWith()函数 判断一个字符串是否以某个字符串结尾

// QT助手
bool QString::endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.QString str = "Bananas";str.endsWith("anas");         // returns truestr.endsWith("pple");         // returns false

(3)QString::contains()函数 判断指定的字符串是否出现过

// QT助手
bool QString::contains(const QString &str, Qt::CaseSensitivity cs = ...) const
Returns true if this string contains an occurrence of the string str; otherwise returns false.
If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
Example:QString str = "Peter Pan";str.contains("peter", Qt::CaseInsensitive);    // returns true

(4)比较字符串

操作符< 、<=、 ==、>=

函数**localeAwareCompare(const QString &s1, const QString &s2)**,静态函数s1小于s2返回负整数,等于返回0,大于返回正整数。比较是基于本地字符集的,平台相关

// QT助手
[static] int QString::localeAwareCompare(const QString &s1, const QString &s2)
Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.
The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.
On macOS and iOS this function compares according the "Order for sorted lists" setting in the International preferences panel.

函数**compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)**

// QT助手
[static] int QString::compare(const QString &s1, const QString &s2, Qt::CaseSensitivity cs = Qt::CaseSensitive)
Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.
If cs is Qt::CaseSensitive, the comparison is case sensitive; otherwise the comparison is case insensitive.
Case sensitive comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-visible strings with localeAwareCompare().int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive);  // x == 0int y = QString::compare("auto", "Car", Qt::CaseSensitive);     // y > 0int z = QString::compare("auto", "Car", Qt::CaseInsensitive);   // z < 0

字符串转换

转换为数值类型

QString::toInt()QString::toDouble()QString::toFloat()QString::toLong()QString::toLongLong()

// QT助手
int QString::toInt(bool *ok = nullptr, int base = 10) const
Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36, or 0. Returns 0 if the conversion fails.
If ok is not nullptr, failure is reported by setting *ok to false, and success by setting *ok to true.
If base is 0, the C language convention is used: If the string begins with "0x", base 16 is used; if the string begins with "0", base 8 is used; otherwise, base 10 is used.
The string conversion will always happen in the 'C' locale. For locale dependent conversion use QLocale::toInt()
Example:QString str = "FF";bool ok;int hex = str.toInt(&ok, 16);       // hex == 255, ok == trueint dec = str.toInt(&ok, 10);       // dec == 0, ok == false
This function ignores leading and trailing whitespace.

转换为其他字符集

QString提供的字符集编码集转换函数将会返回一个const char*类型版本的QByteArray,即构造函数QByteArray(const char*)构造的QByteArray对象

// QT助手
QByteArray QString::toAscii() const
This function is obsolete. It is provided to keep old source code working. We strongly advise against using it in new code.
Returns an 8-bit representation of the string as a QByteArray.
This function does the same as toLatin1().
Note that, despite the name, this function does not necessarily return an US-ASCII (ANSI X3.4-1986) string and its result may not be US-ASCII compatible.
See also fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), and QTextCodec.

😚over😚

相关文章:

QString类方法和变量简介(全)

QString类方法和变量简介 操作字符串(|append|insert|sprintf|QString::arg()|prepend|replace|trimmed|simplified)查询字符串(startsWith|endsWith|contains|localeAwareCompare|compare)字符串转换 标准C提供了两种字符串&#xff1a;一种是C语言风格的以"\0"字符…...

中移链控制台对接4A平台功能验证介绍

中移链控制台具备单独的注册登录页面&#xff0c;用户可通过页面注册或者用户管理功能模块进行添加用户&#xff0c;通过个人中心功能模块进行用户信息的修改和密码修改等操作&#xff0c;因业务要求&#xff0c;需要对中移链控制台的用户账号进行集中管理&#xff0c;统一由 4…...

必知的Facebook广告兴趣定位技巧,更准确地找到目标受众

在Facebook广告投放中&#xff0c;兴趣定位是非常重要的一环。兴趣定位不仅可以帮助我们找到我们想要的目标受众&#xff0c;还可以帮助我们避免一些常见的坑。今天&#xff0c;就让我们一起来看看必知的Facebook广告兴趣定位技巧&#xff0c;更准确地找到目标受众。 1.不要只关…...

【MySQL】慢查询+SQL语句优化 (内容源自ChatGPT)

慢查询SQL语句优化 1.什么是慢查询2.优化慢查询3.插入数据优化5.插入数据底层是什么6.页分裂7.页合并8.主键优化方式10.count 优化11.order by优化12.group by 优化13.limit优化14.update 优化15.innodb 三大特征 1.什么是慢查询 慢查询是指执行SQL查询语句所需要的时间较长&a…...

HashMap底层源码解析及红黑树分析

HashMap线程不安全&#xff0c;底层数组链表红黑树 面试重点是put方法&#xff0c;扩容 总结 put方法 HashMap的put方法&#xff0c;首先通过key去生成一个hash值&#xff0c;第一次进来是null&#xff0c;此时初始化大小为16&#xff0c;i (n - 1) & hash计算下标值&a…...

科技云报道:一路狂飙的ChatGPT,是时候被监管了

科技云报道原创。 即使你过去从不关注科技领域&#xff0c;但近期也会被一个由OpenAI&#xff08;美国的一家人工智能公司&#xff09;开发的人工智能聊天机器人“ChatGPT”刷屏。 与上届“全球网红”元宇宙不同&#xff0c;这位新晋的“全能网友”似乎来势更加凶猛。 互联网…...

第四十四章 管理镜像 - 传入日记传输率

文章目录 第四十四章 管理镜像 - 传入日记传输率传入日记传输率镜像数据库状态 第四十四章 管理镜像 - 传入日记传输率 传入日记传输率 在备份和异步成员的镜像成员状态列表下方&#xff0c;自上次刷新镜像监视器以来日志数据从主服务器到达的速率显示在该成员的传入日志传输…...

加密解密学习笔记

加密种类 对称加密&#xff0c;分组对称加密算法 加密算法 AES&#xff08;Advanced Encryption Standard&#xff09;高级加密标准 DES&#xff08;Data Encryption Standard&#xff09;数据加密标准 3DES/Triple DEA (Triple Data Encryption Algorithm) 三重数据加密算…...

Spring 属性填充源码分析(简单实用版)

属性填充 属性填充只有 3 种方式 根据名称填充 根据类型填充 思考什么时候会出现呢&#xff1f;&#xff1f;&#xff1f; 多见于第三方框架与 Spring集成&#xff0c;举例&#xff1a;Mybatis 与 Spring集成&#xff0c;把 Mapper 接口注册为 BeanDefinition 时候就指定了自…...

【机器学习分支】重要性采样(Importance sampling)学习笔记

重要性采样&#xff08;importance sampling&#xff09;是一种用于估计概率密度函数期望值的常用蒙特卡罗积分方法。其基本思想是利用一个已知的概率密度函数来生成样本&#xff0c;从而近似计算另一个概率密度函数的期望值。 想从复杂概率分布中采样的一个主要原因是能够使用…...

三角回文数+123

三角回文数&#xff1a;用户登录 问题描述 对于正整数 n, 如果存在正整数 k 使得 n123⋯kk(k1)/2​, 则 n 称为三角数。例如, 66066 是一个三角数, 因为 66066123⋯363 。 如果一个整数从左到右读出所有数位上的数字, 与从右到左读出所有数位 上的数字是一样的, 则称这个数为…...

JAVA常用的异步处理方法总结

前言 在java项目开发过程中经常会遇到比较耗时的任务&#xff0c;通常是将这些任务做成异步操作&#xff0c;在java中实现异步操作有很多方法&#xff0c;本文主要总结一些常用的处理方法。为了简化&#xff0c;我们就拿一个实际的案例&#xff0c;再用每种方法去实现&#xf…...

GitLab统计代码量

gitlab官方文档&#xff1a;https://docs.gitlab.com/ee/api/index.html 1、生成密钥 登录gitlab&#xff0c;编辑个人资料&#xff0c;设置访问令牌 2、获取当前用户所有可见的项目 接口地址 GET请求 http://gitlab访问地址/api/v4/projects?private_tokenxxx 返回参数 …...

Linux TCP MIB统计汇总

概述 在 linux > 4.7 才将所有TCP丢包收敛到 函数 tcp_drop 中 指标详解 cat /proc/net/netstat 格式化命令 cat /proc/net/netstat | awk (f0) {name$1; i2; while ( i<NF) {n[i] $i; i }; f1; next} (f1){ i2; while ( i<NF){ printf "%s%s %d\n", …...

记录 docker linux部署jar

第一步 web sso user admin 中yml文件还原到阿里mysql数据库 第二步 各个jar进行打包处理 第三步 正式服务器的Jar备份 第四步 拉取以上jar包 到正式服务器中 第五步 查看 docker images 其中 web_service 1.0.2是上一个版本 上一个版本build 镜像命令是这样的&#xff08;需…...

【Linux】教你用进程替换制作一个简单的Shell解释器

本章的代码可以访问这里获取。 由于程序代码是一体的&#xff0c;本章在分开讲解各部分的实现时&#xff0c;代码可能有些跳跃&#xff0c;建议在讲解各部分实现后看一下源代码方便理解程序。 制作一个简单的Shell解释器 一、观察Shell的运行状态二、简单的Shell解释器制作原理…...

onMeasure里如何重置只有1个子view一行满屏, 若有多个自适应一行

onMeasure里如何重置只有1个子view一行满屏, 若有多个自适应一行 可以尝试在 onMeasure 方法中重写 measureChildWithMargins 或 measureChild 方法来实现这个需求。 对于只有一个字的 View,我们可以把它的宽度设为屏幕宽度,高度设为最大高度,这样这个 View 就会占满一整行…...

Postman创建项目 对接口发起请求处理

查看本文之前 您需要理解了解 Postman 的几个简单工作区 如果还没有掌握 可以先查看我的文章 简单认识 Postman界面操作 那么 掌握之后 我们就可以正式来开启我们的接口测试 我们先选择 Collections 我们点上面这个加号 多拉一个项目出来 然后 我们选我们刚加号点出来的项目…...

在Vue3项目中js-cookie库的使用

文章目录 前言1.安装js-cookie库2.引入、使用js-cookie库 前言 今天分享一下在Vue3项目中引入使用js-cookie。 1.安装js-cookie库 js-cookie官网 安装js-cookie&#xff0c;输入 npm i js-cookie安装完成可以在package.json中看到&#xff1a; 安装以后&#xff0c;就可…...

【论文笔记】Attention和Visual Transformer

Attention和Visual Transformer Attention和Transformer为什么需要AttentionAttention机制Multi-head AttentionSelf Multi-head Attention&#xff0c;SMA TransformerVisual Transformer&#xff0c;ViT Attention和Transformer Attention机制在相当早的时间就已经被提出了&…...

Java 语言特性(面试系列2)

一、SQL 基础 1. 复杂查询 &#xff08;1&#xff09;连接查询&#xff08;JOIN&#xff09; 内连接&#xff08;INNER JOIN&#xff09;&#xff1a;返回两表匹配的记录。 SELECT e.name, d.dept_name FROM employees e INNER JOIN departments d ON e.dept_id d.dept_id; 左…...

大型活动交通拥堵治理的视觉算法应用

大型活动下智慧交通的视觉分析应用 一、背景与挑战 大型活动&#xff08;如演唱会、马拉松赛事、高考中考等&#xff09;期间&#xff0c;城市交通面临瞬时人流车流激增、传统摄像头模糊、交通拥堵识别滞后等问题。以演唱会为例&#xff0c;暖城商圈曾因观众集中离场导致周边…...

【配置 YOLOX 用于按目录分类的图片数据集】

现在的图标点选越来越多&#xff0c;如何一步解决&#xff0c;采用 YOLOX 目标检测模式则可以轻松解决 要在 YOLOX 中使用按目录分类的图片数据集&#xff08;每个目录代表一个类别&#xff0c;目录下是该类别的所有图片&#xff09;&#xff0c;你需要进行以下配置步骤&#x…...

JUC笔记(上)-复习 涉及死锁 volatile synchronized CAS 原子操作

一、上下文切换 即使单核CPU也可以进行多线程执行代码&#xff0c;CPU会给每个线程分配CPU时间片来实现这个机制。时间片非常短&#xff0c;所以CPU会不断地切换线程执行&#xff0c;从而让我们感觉多个线程是同时执行的。时间片一般是十几毫秒(ms)。通过时间片分配算法执行。…...

什么是Ansible Jinja2

理解 Ansible Jinja2 模板 Ansible 是一款功能强大的开源自动化工具&#xff0c;可让您无缝地管理和配置系统。Ansible 的一大亮点是它使用 Jinja2 模板&#xff0c;允许您根据变量数据动态生成文件、配置设置和脚本。本文将向您介绍 Ansible 中的 Jinja2 模板&#xff0c;并通…...

Unsafe Fileupload篇补充-木马的详细教程与木马分享(中国蚁剑方式)

在之前的皮卡丘靶场第九期Unsafe Fileupload篇中我们学习了木马的原理并且学了一个简单的木马文件 本期内容是为了更好的为大家解释木马&#xff08;服务器方面的&#xff09;的原理&#xff0c;连接&#xff0c;以及各种木马及连接工具的分享 文件木马&#xff1a;https://w…...

Hive 存储格式深度解析:从 TextFile 到 ORC,如何选对数据存储方案?

在大数据处理领域&#xff0c;Hive 作为 Hadoop 生态中重要的数据仓库工具&#xff0c;其存储格式的选择直接影响数据存储成本、查询效率和计算资源消耗。面对 TextFile、SequenceFile、Parquet、RCFile、ORC 等多种存储格式&#xff0c;很多开发者常常陷入选择困境。本文将从底…...

NXP S32K146 T-Box 携手 SD NAND(贴片式TF卡):驱动汽车智能革新的黄金组合

在汽车智能化的汹涌浪潮中&#xff0c;车辆不再仅仅是传统的交通工具&#xff0c;而是逐步演变为高度智能的移动终端。这一转变的核心支撑&#xff0c;来自于车内关键技术的深度融合与协同创新。车载远程信息处理盒&#xff08;T-Box&#xff09;方案&#xff1a;NXP S32K146 与…...

Python基于历史模拟方法实现投资组合风险管理的VaR与ES模型项目实战

说明&#xff1a;这是一个机器学习实战项目&#xff08;附带数据代码文档&#xff09;&#xff0c;如需数据代码文档可以直接到文章最后关注获取。 1.项目背景 在金融市场日益复杂和波动加剧的背景下&#xff0c;风险管理成为金融机构和个人投资者关注的核心议题之一。VaR&…...

GitFlow 工作模式(详解)

今天再学项目的过程中遇到使用gitflow模式管理代码&#xff0c;因此进行学习并且发布关于gitflow的一些思考 Git与GitFlow模式 我们在写代码的时候通常会进行网上保存&#xff0c;无论是github还是gittee&#xff0c;都是一种基于git去保存代码的形式&#xff0c;这样保存代码…...