我是靠谱客的博主 无辜黑猫,这篇文章主要介绍QString的一些用法总结(1 section, split 函数),现在分享给大家,希望可以做个参考。

Title :

  • QString
  • QString::section()
  • QString::split()

Q :

如何从一段由特殊符号分隔的 QString 中获取被分隔的子串?

  • 从字符串 “one, two, three, four”中获取第二个由‘,’分隔的子串,即 “two” ;
    复制代码
    1
    1: #include <QtCore/QCoreApplication>
    复制代码
    1
    2: #include <iostream>
    复制代码
    1
    3: using namespace std;
    复制代码
    1
    4:?
    复制代码
    1
    5: int main()
    复制代码
    1
    6: {
    复制代码
    1
    7: QString str = "one, two, three, four";
    复制代码
    1
    8: cout << str.section(',', 1, 1).trimmed().toStdString() << endl;
    复制代码
    1
    9: return 0;
    复制代码
    1
    10: }
结果是 “two”,前后不包含空格。上面的函数 trimmed() 是去掉字符串前后的ASCII字符 't', 'n', 'v', 'f', 'r', and ' ',这些字符用QChar::isSpace()判断都返回true。重点是 section()函数,见下面详细。
  • 从字符串 “one, two* three / four / five ^ six”中获取第四个由 ‘,’、‘*’、 ‘/’和 ‘^’分隔的子串,即“four”;
    复制代码
    1
    1: #include <QtCore/QCoreApplication>
    复制代码
    1
    2: #include <QRegExp>
    复制代码
    1
    3: #include <iostream>
    复制代码
    1
    4: using namespace std;
    复制代码
    1
    5:?
    复制代码
    1
    6: int main()
    复制代码
    1
    7: {
    复制代码
    1
    8: QString str = "one, two* three / four / five ^ six";
    复制代码
    1
    9: cout << str.section(QRegExp("[,*/^]"), 3, 3).trimmed().toStdString() << endl;
    复制代码
    1
    10: return 0;
    复制代码
    1
    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 ,返回的是[ startend ]内的块组成的字符串,如果 start 和 end 都是负数,那么将从字符串的后面往前面数,返回 [ -end–start ]内的块组成的字符串。SectionFlags是一些标记,如SectionSkipEmpty表示如果两个分隔符之间是空串,那么就会跳过。下面的代码是摘自QtDoc 4.7:

  • 复制代码
    1
    1: QString str;
    复制代码
    1
    2: QString csv = "forename,middlename,surname,phone";
    复制代码
    1
    3: QString path = "/usr/local/bin/myapp"; // First field is empty
    复制代码
    1
    4: QString::SectionFlag flag = QString::SectionSkipEmpty;
    复制代码
    1
    5:?
    复制代码
    1
    6: str = csv.section(',', 2, 2); // str == "surname"
    复制代码
    1
    7: str = path.section('/', 3, 4); // str == "bin/myapp"
    复制代码
    1
    8: str = path.section('/', 3, 3, flag); // str == "myapp"
    复制代码
    1
    9:?
    复制代码
    1
    10: str = csv.section(',', -3, -2); // str == "middlename,surname"
    复制代码
    1
    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
1: #include <QtCore/QCoreApplication>
复制代码
1
2: #include <QRegExp>
复制代码
1
3: #include <QStringList>
复制代码
1
4: #include <iostream>
复制代码
1
5: using namespace std;
复制代码
1
6:?
复制代码
1
7: int main()
复制代码
1
8: {
复制代码
1
9: QString str = "one, two* three / four / five ^ six";
复制代码
1
10: QStringList sections = str.split(QRegExp("[,*/^]")); //把每一个块装进一个QStringList中
复制代码
1
11: cout << sections.at(3).trimmed().toStdString() << endl;
复制代码
1
12: return 0;
复制代码
1
13: }


End:

Author : Ggicci

多谢阅读,有误希望指正!

http://tmjfzy.blog.163.com/blog/static/664470252012645536321/

最后

以上就是无辜黑猫最近收集整理的关于QString的一些用法总结(1 section, split 函数)的全部内容,更多相关QString的一些用法总结(1内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部