Qt字符串分割

Qt字符串分割方式根据QT官方助手描述如下两种方法:

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"]

 

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值