QString常用函数介绍

此篇博客核心介绍QT中的QString类型的常用函数,介绍到的函数均从帮助手册或其他博客中看到

QString 字符串类

Header:

#include

qmake:

QT += core

一、QString字符串转换

1、QString类字符串转换为整数

int toInt(bool *ok = Q_NULLPTR, int base = 10)    const
long    toLong(bool *ok = Q_NULLPTR, int base = 10)    const
short    toShort(bool *ok = Q_NULLPTR, int base = 10)    const
uint    toUint(bool *ok = Q_NULLPTR, int base = 10)    const
ulong    toUlong(bool *ok = Q_NULLPTR, int base = 10)    const

注:上述函数参数缺省时,缺省表示从十进制的字符串转换为整型,如果指定参数,则可以将二进制、十六进制字符串转换为整数。

2、QString类字符串转换为浮点数

double    toDouble(bool *ok = Q_NULLPTR)    const
float    toFloat(bool *ok = Q_NULLPTR)    const

3、整数转换为不同进制的QString类字符串

Qstring    &setNum(int n, int base = 10)
Qstring    number(int n,int base = 10)

二、QString字符串内容修改

1、在QString后面添加字符串

QString &append(const QString &s)

2、在QString前面添加字符串

QString &prepend(const QString &s)

3、在QString中替换指定字符串

QString &replace(int i, int len, const QString &after)
QString &replace(const QString &before, QLatin1String after, Qt::CaseSensitivity cs = Qt::CaseSensitive)

4、在QString中插入指定字符串

QString &insert(int i, const QString &s)

5、删除QString中最后几个字符

void chop(int n)

6、删除QString中间几个字符

QString &remove(int i, int len)
QString &remove(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive)

三、QString字符串大小写转换

1、QString内的字母全部转为大写

QString toUpper() const

2、QString内的字母全部转为小写

QString toLower() const

四、返回QString字符串的字符个数

int count()    const
int size()    const
int length()    const

五、去掉QString空格

1、去掉QString首尾空格

QString trimmed()    const

2、去掉QString内所有空格

QString simplified() const

六、QString内字符串的位置

1、从QString特定位置开始查找字符串位置,可指定是否区分大小写

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

注:from:开始查找的位置 cs:是否区分大小写

2、查找某个字符串最后出现的位置

int lastIndexOf(const QString &s, int from = -1, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

七、判断字符串是否为空

bool isNull()
bool isEmpty()
//注:如果字符串内只有"\0",isNull()返回false,isEmpty() 返回true

八、判断QString内容

1、判断QString内是否包含某个字符串

bool contains(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

//注:c:要查询的字符串 cs:大小写

2、判断QString是否以某个字符串开头

bool startsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

3、判断QString是否以某个字符串结尾

bool endsWith(const QString &s, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

九、从QString中取字符串

1、从字符串中左边取指定数量的字符,结果返回字符串

QString left(int n) const

2、从字符串中右边取指定数量的字符,结果返回字符串

QString right(int n) const

3、以指定字符串作为分隔符,分割字符串

QString section(const QString &in_sep, int start, int end = -1, SectionFlags flags = SectionDefault) const
//从字符串中提取in_sep作为分隔符,分割从start端到end端的字符串。

十、构造函数

QString() :默认构造函数 
QString(const char *str) :带参构造,把str字符串的内容赋值给QString 

十一、其余常用函数接口

void clear() //清空字符串 QString QString::mid(int position, int n = -1) //取中间字符串    QStringList split(const QString &sep) //切割后返回字符串链表  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/72196.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

一起创建Vue脚手架吧

目录 一、安装Vue CLI1.1 配置 npm 淘宝镜像1.2 全局安装1.3 验证是否成功 二、创建vue_test项目2.1 cmd进入桌面2.2 创建项目2.3 运行项目2.4 查看效果 三、脚手架结构分析3.1 文件目录结构分析3.2 vscode终端打开项目 一、安装Vue CLI CLI:command-line interface…

一次Linux中的木马病毒解决经历(6379端口---newinit.sh)

病毒入侵解决方案 情景 最近几天一直CPU100%,也没有注意看到了以为正常的服务调用,直到腾讯给发了邮件警告说我的服务器正在入侵其他服务器的6379端口,我就是正常的使用不可能去入侵别人的系统的,这是违法的. 排查 既然入侵6379端口,就怀疑是通过我的Redis服务进入的我的系统…

MySQL的安装以及卸载

下载官网 https://www.mysql.com/ 切到下载tab页 找到 MySQL Community Server 或者 MySQL Community (GPL) Downloads --> MySQL Community Server 点击download按钮: 点击download进入下载页面选择No thanks, just start my download就可以开始下载了。 下…

Android3:布局

一。线性布局 创建项目Linear Layout Example activity_main.xml <?xml version"1.0" encoding"utf-8"?><LinearLayout xmlns:android"http://schemas.android.com/apk/res/android"android:layout_width"match_parent"an…

足部IMU在复杂场景中行走定位

随着微机电系统&#xff08;MEMS&#xff09;技术的快速发展&#xff0c;基于MEMS的惯性导航系统&#xff08;INS&#xff09;在任意环境的基站定位方面发挥着至关重要的作用。惯性导航具有自主性强、定位频率高、功耗低、实时性强等特点。因此更适合单兵作战、反恐行动、应急救…

【推荐】深入浅出benan的生命周期

目录 1.spring 管理JavaBean的过程&#xff08;生命周期&#xff09; 2.spring的JavaBean管理中单例模式及原型&#xff08;多例&#xff09;模式 2.1 . 默认为单例&#xff0c;但是可以配置多例 2.2.举例论证 2.2.1 默认单例 2.2.2 设置多例 2.2.3单例与多例的初始化的时…

常用的分布式计算引擎

记录一下&#xff0c;作为备忘。 常用的分布式计算引擎 多表关联的问题&#xff0c;由于NoSQL数据库主要用于海量存储和单表查询&#xff0c;一般都不支持join&#xff0c;需借助更上层的计算框架来实现多表关联&#xff0c;比如: 计算框架支持数据源执行效率Hive本地文件、…

idea常见错误大全之:解决全局搜索失效+搜索条件失效(条件为空)+F8失灵

问题一&#xff1a;全局搜索快捷键ctrlshiftf 突然失灵了&#xff0c;键盘敲烂了 都没反应&#xff0c;这是为什么呢&#xff1f; 肯定不是idea本身的原因&#xff0c;那么就是其它外在因素影响到了idea的快捷键&#xff0c;那么其它的快捷键为什么没失效呢&#xff0c;原因只有…

PHP8的字符串操作2-PHP8知识详解

今日继续分享《php8的字符串操作》昨天一天都没有写多少&#xff0c;内容多&#xff0c;今天继续&#xff1a; 昨天分享的是1、使用trim()、rtrim()和ltrim()函数去除字符串首尾空格和特殊字符。2、使用strlen()函数和mb_strlen()函数获取字符串的长度。 3、截取字符串 PHP对…

SENet网络分析

文章目录 注意力机制&#xff1a;AttentionBiased Competition Theorybottom-up和top-down注意力 SE BlockSqueeze操作Excitation操作scale操作与原结构合并计算复杂度评估 实验与其他网络对比数据集实验内部参数对比实验进一步评估Squeezeexcitation Squuze-and-Excitation网络…

Pytest和Unittest测试框架的区别?

如何区分这两者&#xff0c;很简单unittest作为官方的测试框架&#xff0c;在测试方面更加基础&#xff0c;并且可以再次基础上进行二次开发&#xff0c;同时在用法上格式会更加复杂&#xff1b;而pytest框架作为第三方框架&#xff0c;方便的地方就在于使用更加灵活&#xff0…

vue3动态组件

1 、 可以通过 shallowRef 把 可以把组件进行包裹 <template><div><el-button click"setclick(son1)">1111</el-button><el-button click"setclick(son2)">2222</el-button><el-button click"setclick(son…