Title :
- QString
- QString::section()
- QString::split()
Q :
如何从一段由特殊符号分隔的 QString 中获取被分隔的子串?
- 从字符串 “one, two, three, four”中获取第二个由‘,’分隔的子串,即 “two” ;
复制代码11: #include <QtCore/QCoreApplication>复制代码12: #include <iostream>复制代码13: using namespace std;复制代码14:?复制代码15: int main()复制代码16: {复制代码17: QString str = "one, two, three, four";复制代码18: cout << str.section(',', 1, 1).trimmed().toStdString() << endl;复制代码19: return 0;复制代码110: }
结果是 “two”,前后不包含空格。上面的函数 trimmed() 是去掉字符串前后的ASCII字符 't', 'n', 'v', 'f', 'r', and ' ',这些字符用QChar::isSpace()判断都返回true。重点是 section()函数,见下面详细。
- 从字符串 “one, two* three / four / five ^ six”中获取第四个由 ‘,’、‘*’、 ‘/’和 ‘^’分隔的子串,即“four”;
复制代码11: #include <QtCore/QCoreApplication>复制代码12: #include <QRegExp>复制代码13: #include <iostream>复制代码14: using namespace std;复制代码15:?复制代码16: int main()复制代码17: {复制代码18: QString str = "one, two* three / four / five ^ six";复制代码19: cout << str.section(QRegExp("[,*/^]"), 3, 3).trimmed().toStdString() << endl;复制代码110: return 0;复制代码111: }上面用到了一个简单的正则表达式,在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:
-
复制代码11: QString str;复制代码12: QString csv = "forename,middlename,surname,phone";复制代码13: QString path = "/usr/local/bin/myapp"; // First field is empty复制代码14: QString::SectionFlag flag = QString::SectionSkipEmpty;复制代码15:?复制代码16: str = csv.section(',', 2, 2); // str == "surname"复制代码17: str = path.section('/', 3, 4); // str == "bin/myapp"复制代码18: str = path.section('/', 3, 3, flag); // str == "myapp"复制代码19:?复制代码110: str = csv.section(',', -3, -2); // str == "middlename,surname"复制代码111: 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() 函数对上述两个问题中的第二个进行求解:
复制代码11: #include <QtCore/QCoreApplication>复制代码12: #include <QRegExp>复制代码13: #include <QStringList>复制代码14: #include <iostream>复制代码15: using namespace std;复制代码16:?复制代码17: int main()复制代码18: {复制代码19: QString str = "one, two* three / four / five ^ six";复制代码110: QStringList sections = str.split(QRegExp("[,*/^]")); //把每一个块装进一个QStringList中复制代码111: cout << sections.at(3).trimmed().toStdString() << endl;复制代码112: return 0;复制代码113: }
End:
Author : Ggicci
多谢阅读,有误希望指正!
http://tmjfzy.blog.163.com/blog/static/664470252012645536321/
最后
以上就是无辜黑猫最近收集整理的关于QString的一些用法总结(1 section, split 函数)的全部内容,更多相关QString的一些用法总结(1内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复