Qt学习之路7--字符串类QString

2024-04-21 01:18

本文主要是介绍Qt学习之路7--字符串类QString,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

字符串的概念在C语言中就存在,有一个历史遗留问题,就是C语言其实是不支持真正意义上的字符串,它是通过字符数组和一组函数实现字符串的操作。
但是在C到C++的进化之后C++已经支持通过定义类来定义自定义类型,这样就可以自定义字符串类,但是在C++发布时一同发布了STL标准库,其中包含了字符串类std::string类类型。

STL全名叫标准模板库,其标准体现在三个方面

  • 相同的全局函数
  • 相同的算法类和数据结构类
  • 相同的类成员函数
    因为STL库的实现依赖于不同厂商实现的编译器,在实现上可能存在差异,所以依赖STL开发的C++程序在不同的平台上行为可能存在差异!!!

Qt中的字符串类

  • 采用Unicode编码,所以一个QChar占用两个字节
  • 使用隐式共享技术来节省内存和减少不必要的数据拷贝
  • 跨平台使用,不用考虑字符串的平台兼容性
  • QString直接支持字符串和数字之间的相互转换
  • QString直接支持字符串之间的大小比较(按照字典序)
  • QString直接支持不同编码下的字符串转换
  • QString直接支持std::string和std::wstring之间的相互转换
  • QString直接支持正则表达式的使用

QString常用操作

包括字符串类对象构造、追加字符串、组合字符串、插入及替换、查找字符获取索引、字符串提取、向其他类型转换、比较、判断字符串是否存在、分隔字符串、过滤空白字符串、大小写切换、判断是否以某个字符串开始或结束、提取字符串、获取长度、

QString对象构造

- QString ( const QChar * unicode, int size )//使用QChar数组中的size长度个字符构造QString
- QString ( const QChar * unicode )//使用QChar数组构造QString,结尾以'\0'结束
- QString ( QChar ch )//使用一个QChar字符构造QString
- QString ( int size, QChar ch )//使用size个ch构造QString
- QString ( const QLatin1String & str )//使用**单字节编码**的str构造QString
- QString ( const QString & other )//使用其他QString引用构造新的QQString
- QString ( const char * str )//使用字符串常量构造QString
- QString ( const QByteArray & ba )//使用字节数组构造QString

追加字符串

QString s = "str";
s += "ing";//s = "string"
s.append(" ");//s = "string "类似于push_back
s.append("test");//向后追加:s = "string test"
s.prepend("This is ");//向前追加:s = "This is string Test"类似于push_front

也可以使用push_back往字符串末尾添加子串,能达到一样的作用

组合字符串

可以使用sprintf函数和arg函数

QString ss.sprintf("%s %.1f%%", "Value", 100.0);//s = "Value 100.0%
s = QString("%1 %2 %3 %4").arg("This").arg("is").arg("a").arg("test");//s = "This is a test"

插入及替换

插入使用insert,替换使用replace

QString& insert ( int position, const QString & str )
QString& insert ( int position, const QLatin1String & str )
QString& insert ( int position, const QChar * unicode, int size )
QString& insert ( int position, QChar ch )

参数position表示插入的位置,从0开始计算
其他参数表示待插入的类型,在构造函数QString时有讲

QString&    replace ( int position, int n, const QString & after )
QString &   replace ( int position, int n, const QChar * unicode, int size )
QString &   replace ( int position, int n, QChar after )//替换成n个QChar

参数position同样表示开始替换的位置
参数n表示替换的长度
其他参数表示待替换的类型,在构造QString时有讲
在Qt的帮助文档中还提供了另外的几组replace函数,使用时可查文档

查找字符串获取第一次出现位置索引

使用indexof函数获取索引

int indexOf ( const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
int indexOf ( const QLatin1String & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
int indexOf ( QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
int indexOf ( const QRegExp & rx, int from = 0 ) const
int indexOf ( QRegExp & rx, int from = 0 ) const

函数的作用是返回待查找字符串第一次出现的位置,没有找到目标字符串返回-1。
str表示待查找的各种形式表示的字符或字符串
QRegExp为通过正则表达式匹配的规则查找
from表示开始查找的位置,如果from = -1表示从最后一个字符开始,如果from = -2表示从倒数第二个开始,以此类推。
Qt::CaseSensitive表示区分大小写
Qt::CaseInsensitive不区分大小写

QString str = "the minimum";//官方例子
str.indexOf(QRegExp("m[aeiou]"), 0);       // returns 4QString x = "std::string & QString";
QString y = "ing";
x.indexOf(y);               // returns 8
x.indexOf(y, 1);            // returns 8
x.indexOf(y, 10);           // returns 18
x.indexOf(y, 19);           // returns -1

清除指定子串

QString &   remove ( QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive )
QString &   remove ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive )QString s = "Hello World";
QString ss = s.remove("l");//ss = "Heo Word"

在母串中删除指定子串或字符

字符串提取

使用mid函数进行提取

//从position开始截取n个字符并返回
QString mid ( int position, int n = -1 ) const
//不使用默认参数
QString s = "QString";
QString ss = s.mid(13);//ss = "Str"//省略第二个参数表示从position开始截取到末尾
QString s = "QString";
QString ss = s.mid(1);//ss = "String"

使用left和right函数提取

QString left(int n)const
QString right(int n)constQString s = "QString";
QString ss = s.left(4);//ss = "QStr"QString s = "QString";
QString ss = s.right(3);//ss = "ing"

函数分别表示从最左或最后提取n个长度的字符串并返回

从字符串到其他类型

使用的是toInt(), toLongLong(), toDouble()…等等。

QString str = "12";
int i = str.toInt();//i = 12

其他转换函数使用方法一致。

数字到字符串

静态函数number或者setNumber

QString s = QString::number(100.0);//参数包含int double long等等
QString ss;
ss.setNum(100.0);

比较字符串

包括不等于、等于、大于、小于、大于等于和小于等于

bool    operator!= ( const QString & other ) const
bool    operator< ( const QString & other ) const
bool    operator<= ( const QString & other ) const
bool    operator== ( const QString & other ) const
bool    operator> ( const QString & other ) const
bool    operator>= ( const QString & other ) const

参数表示被比较的字符串
其他重载版本查看帮助文档

判断字符串是否存在

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

判断字符串是否是empty

bool    isEmpty () const//原型QString().isEmpty();            // returns true
QString("").isEmpty();          // returns true
QString("x").isEmpty();         // returns false
QString("abc").isEmpty();       // returns false

它判断字符串是不是空串,等价于if(QString(” “).length() == 0)

判断字符串是否NULL

bool    isNull () const//原型QString().isNull();             // returns true
QString("").isNull();           // returns false
QString("abc").isNull();        // returns false

只有当构造QString不带任何参数时返回true

分隔字符串

通过使用QString::split()能够将一个大串按照某个子串划分成多个子串并存到QStringList中。

QString str = "You,I,She";
QStringList list= str.split(",");

结果list.at(0) = “You” list.at(1) = “I” list.at(2) = “She”

过滤空白字符串

QString trimmed () const//原型QString s(" abc def ghi ");
s = s.trimmed();//s = "abc def ghi"

通过调用该函数可以去除首尾的空格

大小写切换

切换为全大写,使用toUpper()函数

QString s = "Hello World";
QString ss = s.toUpper();

切换为全小写,使用toLower()函数

QString s = "Hello World";
QString ss = s.toLower();

判断是否以某个字符串开始或结束

(1)以某子串开始,使用startsWith()函数

QString s = "http:www.baidu.com";
bool i = s.startsWith("http:");

结果为i = true;

(2)以某子串结束,使用endsWith()函数

QString str = "http:www.baidu.com";
bool i = str.endsWith("com");

结果为i = true;

获取子串出现的次数

//原型
int QString::count ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
//示例
QString s = "Hello World";
qDebug() << s.count("l");// 输出3

结果返回的是str在母串中出现的次数

获取字符串长度

int QString::length () const//原型//示例QString s = "Hello World";
qDebug() << s.length();//输出11,说明不带'\0'

这篇关于Qt学习之路7--字符串类QString的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/921825

相关文章

Qt使用QSqlDatabase连接MySQL实现增删改查功能

《Qt使用QSqlDatabase连接MySQL实现增删改查功能》这篇文章主要为大家详细介绍了Qt如何使用QSqlDatabase连接MySQL实现增删改查功能,文中的示例代码讲解详细,感兴趣的小伙伴... 目录一、创建数据表二、连接mysql数据库三、封装成一个完整的轻量级 ORM 风格类3.1 表结构

Python中反转字符串的常见方法小结

《Python中反转字符串的常见方法小结》在Python中,字符串对象没有内置的反转方法,然而,在实际开发中,我们经常会遇到需要反转字符串的场景,比如处理回文字符串、文本加密等,因此,掌握如何在Pyt... 目录python中反转字符串的方法技术背景实现步骤1. 使用切片2. 使用 reversed() 函

Qt QCustomPlot库简介(最新推荐)

《QtQCustomPlot库简介(最新推荐)》QCustomPlot是一款基于Qt的高性能C++绘图库,专为二维数据可视化设计,它具有轻量级、实时处理百万级数据和多图层支持等特点,适用于科学计算、... 目录核心特性概览核心组件解析1.绘图核心 (QCustomPlot类)2.数据容器 (QCPDataC

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

MySQL 获取字符串长度及注意事项

《MySQL获取字符串长度及注意事项》本文通过实例代码给大家介绍MySQL获取字符串长度及注意事项,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录mysql 获取字符串长度详解 核心长度函数对比⚠️ 六大关键注意事项1. 字符编码决定字节长度2

Qt如何实现文本编辑器光标高亮技术

《Qt如何实现文本编辑器光标高亮技术》这篇文章主要为大家详细介绍了Qt如何实现文本编辑器光标高亮技术,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录实现代码函数作用概述代码详解 + 注释使用 QTextEdit 的高亮技术(重点)总结用到的关键技术点应用场景举例示例优化建议

Qt 设置软件版本信息的实现

《Qt设置软件版本信息的实现》本文介绍了Qt项目中设置版本信息的三种常用方法,包括.pro文件和version.rc配置、CMakeLists.txt与version.h.in结合,具有一定的参考... 目录在运行程序期间设置版本信息可以参考VS在 QT 中设置软件版本信息的几种方法方法一:通过 .pro

Springboot3+将ID转为JSON字符串的详细配置方案

《Springboot3+将ID转为JSON字符串的详细配置方案》:本文主要介绍纯后端实现Long/BigIntegerID转为JSON字符串的详细配置方案,s基于SpringBoot3+和Spr... 目录1. 添加依赖2. 全局 Jackson 配置3. 精准控制(可选)4. OpenAPI (Spri

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

使用Python实现base64字符串与图片互转的详细步骤

《使用Python实现base64字符串与图片互转的详细步骤》要将一个Base64编码的字符串转换为图片文件并保存下来,可以使用Python的base64模块来实现,这一过程包括解码Base64字符串... 目录1. 图片编码为 Base64 字符串2. Base64 字符串解码为图片文件3. 示例使用注意