一、字符串处理相关
1.1 split() (拆分字符串)
split() 函数可以将一个字符串,拆分成一个装有多个子串的 QStringList。示例如下:
QString str = "polluter pays principle";
//根据空格拆分字符串
QStringList words = str.split(" ");
qDebug() << words; //returns ("polluter", "pays", "principle")
在上面的例子,我们把 "polluter pays principle" 分成三个子串: "polluter", "pays" 和 "principle"。
1.2 section()(分割字符串)
section() 函数用来以某个字符分割字符串。函数原型为:
QString QString::section (QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault) const
这个函数把字符串看成是几个块,这些块由 sep 分隔,start 和 end 指定块号,end 默认为 –1 ,返回的是[ start, end ]内的块组成的字符串。
下面例子功能:从字符串 “one, two, three, four” 中获取第二个由 ‘,’ 分隔的子串,即 “two”。
QString str = "one, two, three, four";
cout << str.section(',', 1, 1).trimmed().toStdString() << endl; //returns two
结果是 “two”,前后不包含空格。上面的函数 trimmed() 是去掉字符串前后的 ASCII 字符 '\t', '\n', '\v', '\f', '\r' 和 ' '。
再看一个例子,这个例子是从字符串 “one, two* three / four / five ^ six” 中获取由 ‘,’ 和 ‘^’ 分隔的第二个子串,即 “four”;
QString str = "one, two* three / four / five ^ six";
cou
Qstring 常用成员函数总结(split,section,mid......)
最新推荐文章于 2024-11-29 15:19:49 发布
本文深入讲解了Qt中字符串处理的方法,包括split(), section(), mid(), replace()等函数的使用,以及字符串与各种类型间的转换技巧。同时,文章还介绍了如何构建和查询字符串,以及过滤字符串的实用函数。

最低0.47元/天 解锁文章
2054





