我的Qt学习笔记 2 QString的一些用法总结(1 section, split 函数)
来源:互联网 发布:域名邮箱不合法是 编辑:程序博客网 时间:2024/05/19 13:10
Title :
- QString
- QString::section()
- QString::split()
Q :
如何从一段由特殊符号分隔的 QString 中获取被分隔的子串?
- 从字符串 “one, two, three, four”中获取第二个由‘,’分隔的子串,即 “two” ;
1: #include <QtCore/QCoreApplication>
2: #include <iostream>
3: using namespace std;
4:?
5: int main()
6: {
7: QString str = "one, two, three, four";
8: cout << str.section(',', 1, 1).trimmed().toStdString() << endl;
9: return 0;
10: }
结果是 “two”,前后不包含空格。上面的函数 trimmed() 是去掉字符串前后的ASCII字符 '\t', '\n', '\v', '\f', '\r', and ' ',这些字符用QChar::isSpace()判断都返回true。重点是 section()函数,见下面详细。
- 从字符串 “one, two* three / four / five ^ six”中获取第四个由 ‘,’、‘*’、 ‘/’和 ‘^’分隔的子串,即“four”;
1: #include <QtCore/QCoreApplication>
2: #include <QRegExp>
3: #include <iostream>
4: using namespace std;
5:?
6: int main()
7: {
8: QString str = "one, two* three / four / five ^ six";
9: cout << str.section(QRegExp("[,*/^]"), 3, 3).trimmed().toStdString() << endl;
10: return 0;
11: }
上面用到了一个简单的正则表达式,在Qt中可以由类QRegExp构造,函数 section() 支持使用正则表达式。
Fn 1 :
QString QString::section ( QChar sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const
这个函数把字符串看成是几个块,这些块由 sep 分隔,start 和 end 指定块号,end 默认为 –1 ,返回的是[ start, end ]内的块组成的字符串,如果 start 和 end 都是负数,那么将从字符串的后面往前面数,返回 [ -end, –start ]内的块组成的字符串。SectionFlags是一些标记,如SectionSkipEmpty表示如果两个分隔符之间是空串,那么就会跳过。下面的代码是摘自QtDoc 4.7:
1: QString str;
2: QString csv = "forename,middlename,surname,phone";
3: QString path = "/usr/local/bin/myapp"; // First field is empty
4: QString::SectionFlag flag = QString::SectionSkipEmpty;
5:?
6: str = csv.section(',', 2, 2); // str == "surname"
7: str = path.section('/', 3, 4); // str == "bin/myapp"
8: str = path.section('/', 3, 3, flag); // str == "myapp"
9:?
10: str = csv.section(',', -3, -2); // str == "middlename,surname"
11: str = path.section('/', -1); // str == "myapp"
- 另外这个函数的另两个重载函数如下:
QString QString::section ( const QString & sep, int start, int end = -1, SectionFlags flags = SectionDefault ) const
QString QString::section ( const QRegExp & reg, int start, int end = -1, SectionFlags flags = SectionDefault ) const
第二个支持正则表达式作为分隔符判定,在某些场合下是不可替代的,如上面的问题中的第二个。
Fn 2 :
QStringList QString::split ( const QChar & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
这个函数把所有的由 sep 分隔的块装进一个 QStringList 中返回, 这个函数同样有两个重载:
QStringList QString::split ( const QString & sep, SplitBehavior behavior = KeepEmptyParts, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
QStringList QString::split ( const QRegExp & rx, SplitBehavior behavior = KeepEmptyParts ) const
使用 split() 函数对上述两个问题中的第二个进行求解:
1: #include <QtCore/QCoreApplication>
2: #include <QRegExp>
3: #include <QStringList>
4: #include <iostream>
5: using namespace std;
6:?
7: int main()
8: {
9: QString str = "one, two* three / four / five ^ six";
10: QStringList sections = str.split(QRegExp("[,*/^]")); //把每一个块装进一个QStringList中
11: cout << sections.at(3).trimmed().toStdString() << endl;
12: return 0;
13: }
End:
Author : Ggicci
0 0
- 我的Qt学习笔记 2 QString的一些用法总结(1 section, split 函数)
- 我的Qt学习笔记 2 QString的一些用法总结(1 section, split 函数)
- QString的一些用法总结(1 section, split 函数)
- QString的一些用法总结(1 section, split 函数)
- QString的一些用法总结
- Qt QString的用法
- Qt学习-QString用法总结( setNum, number 函数)
- Qt学习笔记1-QString
- String的split()函数的用法总结
- QString字符串的split
- Python Split函数的用法总结
- Python Split函数的用法总结
- Python Split函数的用法总结
- Python Split函数的用法总结
- Python Split函数的用法总结
- Python Split函数的用法总结
- Python Split函数的用法总结
- 笔记:boost中split函数的用法
- 使用matlab的guide来创建编辑控件
- WINCE下虚拟串口驱动设计
- 脱离繁杂的地图开发,寻找最简单快捷的地图应用
- 到底小米如何成功的?7大铁规
- [练习题] 一道变态的面试题(吐槽啊吐槽)
- 我的Qt学习笔记 2 QString的一些用法总结(1 section, split 函数)
- IOS6新特性集合视图<UICollectionView>CircleLayout实践
- 使用github:windows/linux
- 如何监控并找出系统中存在的大量的未使用绑定变量的SQL
- oracle select 走索引?走全表扫描?
- Mahout算法集
- c++ 指针和引用的区别
- EffectiveSTL笔记二-vector和string
- Unity3D ARPG游戏开发《最初的幻想》之第一张地图与跳跃的改善详解