QString QString::section(QChar sep, int start, int end = -1, 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.
然后对分割结果做如下测试:(大小写区分没有测试)
QString str;
QString path = "/usr//local/bin/"; // First field is empty
//设置section分割操作对于空字串和分隔符的操作
/*
QString::SectionDefault
计算空字段,不包括前导和尾随分隔符,并且区分大小写。
QString::SectionSkipEmpty
无视QString空串,不包括前导和尾随分隔符。
QString::SectionIncludeLeadingSep
在结果字符串中包含前导分隔符(如果有前导分隔符)。
QString::SectionIncludeTrailingSep
在结果字符串中包含尾随分隔符(如果有尾随分隔符)。
QString::SectionCaseInsensitiveSeps
比较分隔符不区分大小写。
*/
QString::SectionFlag flag1 = QString::SectionDefault;
QString::SectionFlag flag2 = QString::SectionSkipEmpty;
QString::SectionFlag flag3 = QString::SectionIncludeLeadingSep;
QString::SectionFlag flag4 = QString::SectionIncludeTrailingSep;
QString::SectionFlag flag5 = QString::SectionCaseInsensitiveSeps;
str = path.section('/', 0, -1, flag1); // str == "/usr//local/bin/"
str = path.section('/', 0, -1, flag2); // str == "usr//local/bin"
str = path.section('/', 0, -1, flag3); // str == "/usr//local/bin/"
str = path.section('/', 0, -1, flag4); // str == "/usr//local/bin/"
str = path.section('/', 0, -1, flag5); // str == "/usr//local/bin/"
str = path.section('/', 0, 0, flag1); // str == ""
str = path.section('/', 0, 0, flag2); // str == "usr"
str = path.section('/', 0, 0, flag3); // str == ""
str = path.section('/', 0, 0, flag4); // str == "/"
str = path.section('/', 0, 0, flag5); // str == ""
str = path.section('/', 0, 2, flag1); // str == "/usr/"
str = path.section('/', 0, 2, flag2); // str == "usr//local/bin"
str = path.section('/', 0, 2, flag3); // str == "/usr/"
str = path.section('/', 0, 2, flag4); // str == "/usr//"
str = path.section('/', 0, 2, flag5); // str == "/usr/"
str = path.section('/', 1, 1, flag1); // str == "usr"
str = path.section('/', 1, 1, flag2); // str == "local"
str = path.section('/', 1, 1, flag3); // str == "/usr"
str = path.section('/', 1, 1, flag4); // str == "usr/"
str = path.section('/', 1, 1, flag5); // str == "usr"
str = path.section('/', 2, 2, flag1); // str == ""
str = path.section('/', 2, 2, flag2); // str == "bin"
str = path.section('/', 2, 2, flag3); // str == "/"
str = path.section('/', 2, 2, flag4); // str == "/"
str = path.section('/', 2, 2, flag5); // str == ""
由此可看出,在五个flag的情况下的分割情况应该是:
/*
QString path = "/usr//local/bin/";对/分割的不同flag分割结果如下:
QString::SectionDefault
计算空字段,不包括前导和尾随分隔符,并且区分大小写。
0.""
1."usr"
2.""
3."local"
4."bin"
5.""
QString::SectionSkipEmpty
无视QString空串,不包括前导和尾随分隔符。
0."usr"
1."local"
2."bin"
QString::SectionIncludeLeadingSep
在结果字符串中包含前导分隔符(如果有前导分隔符)。
0.""
1."/usr"
2."/"
3."/local"
4."/bin"
5."/"
QString::SectionIncludeTrailingSep
在结果字符串中包含尾随分隔符(如果有尾随分隔符)。
0."/"
1."usr/"
2."/"
3."local/"
4."bin/"
5.""
QString::SectionCaseInsensitiveSeps
比较分隔符不区分大小写。
0.""
1."usr"
2.""
3."local"
4."bin"
5.""
*/
QStringList QString::split(const QString &sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
Splits the string into substrings wherever sep occurs, and returns the list of those strings. If sep does not match anywhere in the string, split() returns a single-element list containing this string.
cs specifies whether sep should be matched case sensitively or case insensitively.
If behavior is QString::SkipEmptyParts, empty entries don't appear in the result. By default, empty entries are kept.
QString path = "/usr//local/bin/"; // First field is empty
//设置split分割操作对于空字串的操作
/*
QString::KeepEmptyParts
保留空字串
QString::SkipEmptyParts
跳过空字串
*/
QStringList list1 = path.split('/');// list1: ["", "usr", "", "local", "bin", ""]
QStringList list2 = path.split('/', QString::SkipEmptyParts);// list2: ["usr", "local", "bin"]