首页 > 其他分享 >QString::section详解

QString::section详解

时间:2023-06-16 09:45:54浏览次数:31  
标签:end section list start 详解 QString str

目录

section()函数简介

网上有很多关于Qt中字符串工具函数QString::section的描述,但大多描述不够清晰、直接。本文从官方文档入手,详细讲解如何使用section。

QString::section 可用来分隔字符串,与QString::split区别是:前者可只取指定范围的字符串内容,后者返回的是切分后的结果列表,包含所有切分结果字符串。

section()声明

QString::section()有多个重载版本:

QString section(QChar sep, qsizetype start, qsizetype end = -1, QString::SectionFlags flags = SectionDefault) const;
QString section(const QString &sep, qsizetype start, qsizetype end = -1, QString::SectionFlags flags = SectionDefault) const;
QString section(const QRegularExpression &re, qsizetype start, qsizetype end = -1, QString::SectionFlags flags = SectionDefault) const;

我们截取其中最简单的一个帮助文档,其描述如下:

QString QString::section(QChar sep, qsizetype start, qsizetype end = -1, QString::SectionFlags flags = SectionDefault) const

This function returns a section of the string.
This string is treated as a sequence of fields separated by the character, sep. The returned string consists of the fields from position start to position end inclusive. If end is not specified, all fields from position start to the end of the string are included. Fields are numbered 0, 1, 2, etc., counting from the left, and -1, -2, etc., counting from right to left.
The flags argument can be used to affect some aspects of the function's behavior, e.g. whether to be case sensitive, whether to skip empty fields and how to deal with leading and trailing separators; see SectionFlags.

大意是:函数返回字符串的一部分。字符sep将会被用来切分字符串(调用对象)。返还的字符串,是由位置开始start,到end。如果没有指定结束位置,则包括从字符串的位置开始到结束的所有字段。字段编号从中到右数为0,1,2,等,从右到左数为-1,-2,等。
flags参数能用来影响一些函数行为,例如,分隔符是否大小写敏感,是否忽略空字段,如何处理前导、尾缀分隔符。

section()的作用是,在调用对象(QString)中查找sep(QChar),对其进行切分,得到一个字符串列表(比如,list),而start、end就是指定这个列表的索引,最终返回的就是list[start..end],不同list元素之间用分隔符连接。

start, end含义

start、end并非原字符串的索引,而是切分字符串后得到的字符串数组的索引。
例如,"forename,middlename,surname,phone"按','进行切分后,得到4个元素的字符串数组list = {"forname", "middlename", "surname", "phone"} (索引分别为0,1,2,3),
当start = 2, end = 2时,返回"surname";
当start = 2, end = 3时,返回"surname,phone"。

flags参数

参数QString::SectionFlags可以指定不同的值,影响section函数行为:

Constant Value Description
QString::SectionDefault 0x00 Empty fields are counted, leading and trailing separators are not included, and the separator is compared case sensitively.
空的字段也会被计数,前导和尾缀分隔符不被包括,分隔符比较时大小写敏感
QString::SectionSkipEmpty 0x01 Treat empty fields as if they don't exist, i.e. they are not considered as far as start and end are concerned.
空字段当做好像不存在,例如,关注start和end时,空字段不被考虑
QString::SectionIncludeLeadingSep 0x02 Include the leading separator (if any) in the result string.
结果字符串中包含前导分隔符(如果有)
QString::SectionIncludeTrailingSep 0x03 Include the trailing separator (if any) in the result string.
结果字符串中包含尾缀分隔符(如果有
QString::SectionCaseInsensitiveSeps 0x04 Compare the separator case-insensitively.
比较分隔符时,大小写不敏感

示例

对文档中几个例子,进行详细说明:

  • 从左到右计数

1)csv按','切分后,list = {"forename", "middlename", "surname", "phone"}(索引分别为0,1,2,3)
因此,csv.section(',', 2, 2) = list[2..2] = "surname"。

2)path按'/'切分后,list = {"", "usr", "local", "bin", "myapp"}(索引分别为0,1,2,3,4)
因此,path.section('/', 3, 4) = list[3..4] = "bin/myapp"

3)由flags为QString::SectionSkipEmpty,path按'/'切分后,会忽略空字段,所以list = {"usr", "local", "bin", "myapp"}(索引分别为0,1,2,3)
因此,path.section('/', 3, 3, flag) = "myapp"

 QString str;
 QString csv = "forename,middlename,surname,phone";
 QString path = "/usr/local/bin/myapp"; // First field is empty
 QString::SectionFlag flag = QString::SectionSkipEmpty;

 str = csv.section(',', 2, 2);   // str == "surname"
 str = path.section('/', 3, 4);  // str == "bin/myapp"
 str = path.section('/', 3, 3, flag); // str == "myapp"
  • 从右到左计数

1)csv按','切分后,list = {"forename", "middlename", "surname", "phone"}(从右到左索引分别为-1,-2,-3,-4)
因此,csv.section(',', -3, -2) = list[-3..-2] = "middlename,surname"

2)path按'/'切分后,list = {"", "usr", "local", "bin", "myapp"}(从右到左索引分别为-1,-2,-3,-4,-5)
省略end参数,代表使用默认值-1
因此,path.section('/', -1) = list[-1..-1] = "myapp"

 str = csv.section(',', -3, -2);  // str == "middlename,surname"
 str = path.section('/', -1); // str == "myapp"

标签:end,section,list,start,详解,QString,str
From: https://www.cnblogs.com/fortunely/p/17484450.html

相关文章

  • WebP格式详解
    参考:https://zh.wikipedia.org/zh-sg/WebP实战:直播推荐封面使用webp格式部分机型不展示 在开发相亲cny的时候,直播推荐模块server下发的封面图是webp的格式,android展示没有问题,ios14以下不支持webp所以让server做了处理,低版本下发png格式  ......
  • 【回调详解】内核回调的详细图解【未完成】
    1、进程回调进程回调是内核下的全局变量,存放到PspCreateProcessNotifyRoutine中,该变量是个数组;该数组中已经存放函数的具体个数,则存放到全局变量PspCreateProcessNotifyRoutineCount中。PspCreateProcessNotifyRoutine的最大值由一个宏决定:PSP_MAX_CREATE_PROCESS_NOTIFY。1.1......
  • 详解MySQL Server端如何发送结果集给客户端
    MySQLServer和Client之间的交互有一套定义得很明确的协议,称为MySQLClient/ServerProtocol。写数据库的人,只需要遵循这套协议来写程序,就能让自己的数据库被各种MySQL客户端连接,如mysql命令行,phpmysql,JDBC等等。这是一个非常诱人的设计选择(DesignChoice)!如果自己实现一套协议,写......
  • Kubernetes Pod 驱逐详解
    参考网址1参考网址2QoS等级为Guaranteed的Pod会在QoS等级为Burstable的Pod之前被驱逐吗?在Kubernetes中,Pod使用的资源最重要的是CPU、内存和磁盘IO,这些资源可以被分为可压缩资源(CPU)和不可压缩资源(内存,磁盘IO)。可压缩资源不可能导致Pod被驱逐,因为当Pod的......
  • jspsmart详解(转)
    ㈠File类这个类包装了一个上传文件的所有信息。通过它,可以得到上传文件的文件名、文件大小、扩展名、文件数据等信息。File类主要提供以下方法:1、saveAs作用:将文件换名另存。原型:publicvoidsaveAs(java.lang.StringdestFilePathName)或publicvoidsaveAs(java.......
  • 详解spring事务属性
    Spring声明式事务让我们从复杂的事务处理中得到解脱。使得我们再也无需要去处理获得连接、关闭连接、事务提交和回滚等这些操作。再也无需要我们在与事务相关的方法中处理大量的try…catch…finally代码。我们在使用Spring声明式事务时,有一个非常重要的概念就是事务属性。事务......
  • Java正则表达式详解
    如果你曾经用过Perl或任何其他内建正则表达式支持的语言,你一定知道用正则表达式处理文本和匹配模式是多么简单。如果你不熟悉这个术语,那么“正则表达式”(RegularExpression)就是一个字符构成的串,它定义了一个用来搜索匹配字符串的模式。许多语言,包括Perl、PHP、Python、JavaScript......
  • JDBC-API详解-PreparedStatement-原理
    /**PreparedStatement原理讲解*PreparedStatement的预编译功能用useServerPrepStmts=true开启**/@TestpublicvoidPreparedStatement2()throwsException{//1.注册驱动//Class.forName......
  • web.xml加载详解
    一 1、启动一个WEB项目的时候,WEB容器会去读取它的配置文件web.xml,读取<listener>和<context-param>两个结点。2、紧急着,容器创建一个ServletContext(servlet上下文),这个web项目的所有部分都将共享这个上下文。 3、容器将<context-param>转换为键值对,并交给servletContext。 4......
  • JDBC-API详解-PreparedStatement-SQL注入演示
       packageTest;importorg.junit.Test;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importjava.util.ArrayList;importjava.util.List;publicclassJDBCdemo5_UserLogin{/*......