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

【Qt 比较常用的字符串处理函数】查找、比较、转换、截取、替换、插入、删除、格式化、分割、比较排序

在Qt中,QString类提供了许多用于字符串处理的函数。以下是一些常用的字符串处理函数:

字符串查找

  • int indexOf(const QString &str, int from = 0) const:返回子字符串str在字符串中第一次出现的位置,从from位置开始查找。如果未找到,返回-1。
  • int lastIndexOf(const QString &str, int from = -1) const:返回子字符串str在字符串中最后一次出现的位置,从from位置开始反向查找。如果未找到,返回-1。

字符串比较

  • bool operator==(const QString &other) const:比较两个字符串是否相等。
  • bool operator!=(const QString &other) const:比较两个字符串是否不相等。
  • bool compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const:比较两个字符串,可以指定大小写敏感性。

字符串转换

  • QString toUpper() const:将字符串转换为大写。
  • QString toLower() const:将字符串转换为小写。
  • QString toTitleCase() const:将字符串转换为标题大小写(每个单词的首字母大写)。
  • QString toCaseFolded() const:将字符串转换为大小写折叠形式(用于不区分大小写的比较)。

字符串截取

  • QString left(int n) const:返回字符串的前n个字符。
  • QString right(int n) const:返回字符串的最后n个字符。
  • QString mid(int position, int n = -1) const:返回从position开始的n个字符。如果n为-1,则返回从position开始到字符串末尾的所有字符。

字符串替换

  • QString replace(int position, int n, const QString &after):替换从position开始的n个字符为字符串after
  • QString replace(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive):替换所有出现的子字符串before为字符串after,可以指定大小写敏感性。

字符串插入

  • QString insert(int position, const QString &str):在字符串的position位置插入字符串str

字符串删除

  • QString remove(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive):删除所有出现的子字符串str,可以指定大小写敏感性。
  • QString remove(int position, int n):删除从position开始的n个字符。

字符串格式化

  • QString arg(const QVariant &a, int fieldWidth = 0, int base = 10, const QChar &fillChar = QLatin1Char(' ')) const:使用参数替换字符串中的占位符。

字符串分割

  • QStringList split(const QString &sep, Qt::SplitBehavior behavior = Qt::KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const:根据分隔符sep将字符串分割成多个子字符串,返回一个QStringList

字符串比较和排序

  • int compare(const QString &other, Qt::CaseSensitivity cs = Qt::CaseSensitive) const:比较两个字符串,返回-1、0或1,表示小于、等于或大于。
  • QStringList sorted(Qt::CaseSensitivity cs = Qt::CaseSensitive) const:返回一个排序后的字符串列表。

这些函数提供了强大的字符串处理功能,可以帮助你在Qt应用程序中轻松地进行各种字符串操作。

相关文章:

  • VSCode Error Lens插件介绍(代码静态检查与提示工具)(vscode插件)
  • 前端实现在PDF上添加标注(1)
  • D365(Dynamics 365 CE/PO)审计记录(Audit)一点点分析
  • linux下c++连接mysql
  • 数据结构:哈夫曼树
  • 【SpringBoot实现全局API限频】 最佳实践
  • Ceph集群搭建2025(squid版)
  • 【机器学习】线性回归与一元线性回归
  • Oracle日常管理(8)——DB日常管理(1)
  • Python(二十)实现各大跨境船公司物流查询新增[ARKAS Line]船司
  • YOLOv11-ultralytics-8.3.67部分代码阅读笔记-plotting.py
  • 【ESP32接入国产大模型之Deepseek】
  • 2.5 模块化迁移策略:从传统项目到模块化系统
  • 算法——结合实例了解启发式搜索
  • 网络安全的现状如何?
  • LabVIEW 中dde.llbDDE 通信功能
  • linux中top命令详解
  • linux 释放9090端口
  • 多模态识别和自然语言处理有什么区别
  • 100N10-ASEMI小家电专用MOS管100N10
  • 保定网站设计制作/seo免费视频教程
  • 教育app定制开发/金阊seo网站优化软件
  • 杭州pc网站制作公司/搜索引擎优化案例
  • 韶山网站建设/网络营销策划书应该怎么写
  • 广东深圳疫情/win7优化软件
  • 第一次做网站做后感/百度搜索引擎技巧