QString::section详解

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"
posted @ 2023-06-16 09:34  明明1109  阅读(2474)  评论(0编辑  收藏  举报