返回介绍

QStringList Class

发布于 2019-10-04 15:03:04 字数 8467 浏览 1155 评论 0 收藏 0

The QStringList class provides a list of strings. More...

#include <qstringlist.h>

Inherits QValueList<QString>.

List of all member functions.

Public Members

  • QStringList ()
  • QStringList ( constQStringList&l )
  • QStringList ( constQValueList<QString>&l )
  • QStringList ( constQString&i )
  • QStringList ( constchar*i )
  • void sort ()
  • QString join ( constQString&sep ) const
  • QStringList grep ( constQString&str, boolcs = TRUE ) const
  • QStringList grep ( constQRegExp&expr ) const

Static Public Members

  • QStringList fromStrList ( constQStrList&ascii )
  • QStringList split ( constQString&sep, constQString&str, boolallowEmptyEntries = FALSE )
  • QStringList split ( constQChar&sep, constQString&str, boolallowEmptyEntries = FALSE )
  • QStringList split ( constQRegExp&sep, constQString&str, boolallowEmptyEntries = FALSE )

Detailed Description

The QStringList class provides a list of strings.

It is used to store and manipulate strings that logically belong together. Basically QStringList is a QValueList of QString objects. As opposed to QStrList, which stores pointers to characters, QStringList deals with real QString objects. It is the class of choice whenever you work with Unicode strings. QStringList is part of the Qt Template Library.

Like QString itself, QStringList objects are implicitly shared. Passing them around as value-parameters is both fast and safe.

Strings can be added to a list using append(), operator+=() or operator<<(), e.g.

    QStringList fonts;
    fonts.append( "Times" );
    fonts += "Courier";
    fonts += "Courier New";
    fonts << "Helvetica [Cronyx]" << "Helvetica [Adobe]";
    

String lists have an iterator, QStringList::Iterator(), e.g.

    for ( QStringList::Iterator it = fonts.begin(); it != fonts.end(); ++it ) {
        cout << *it << ":";
    }
    cout << endl;
    // Output:
    //  Times:Courier:Courier New:Helvetica [Cronyx]:Helvetica [Adobe]:
    

Many Qt functions return const string lists; to iterate over these you should make a copy and iterate over the copy.

You can concatenate all the strings in a string list into a single string (with an optional separator) using join(), e.g.

    QString allFonts = fonts.join( ", " );
    cout << allFonts << endl;
    // Output:
    //  Times, Courier, Courier New, Helvetica [Cronyx], Helvetica [Adobe]
    

You can sort the list with sort(), and extract a new list which contains only those strings which contain a particular substring (or match a particular regular expression) using the grep() functions, e.g.

    fonts.sort();
    cout << fonts.join( ", " ) << endl;
    // Output:
    //  Courier, Courier New, Helvetica [Adobe], Helvetica [Cronyx], Times

    QStringList helveticas = fonts.grep( "Helvetica" );
    cout << helveticas.join( ", " ) << endl;
    // Output:
    //  Helvetica [Adobe], Helvetica [Cronyx]
    

Existing strings can be split into string lists with character, string or regular expression separators, e.g.

    QString s = "Red\tGreen\tBlue";
    QStringList colors = QStringList::split( "\t", s );
    cout << colors.join( ", " ) << endl;
    // Output:
    //  Red, Green, Blue
    

See also Implicitly and Explicitly Shared Classes, Text Related Classes and Non-GUI Classes.


Member Function Documentation

QStringList::QStringList ()

Creates an empty string list.

QStringList::QStringList ( constQStringList&l )

Creates a copy of the list l. This function is very fast because QStringList is implicitly shared. However, for the programmer this is the same as a deep copy. If this list or the original one or some other list referencing the same shared data is modified, the modifying list first makes a copy, i.e. copy-on-write.

QStringList::QStringList ( constQValueList<QString>&l )

Constructs a new string list that is a copy of l.

QStringList::QStringList ( constQString&i )

Constructs a string list consisting of the single string i. Longer lists are easily created as follows:

    QStringList items;
    items << "Buy" << "Sell" << "Update" << "Value";
    

QStringList::QStringList ( constchar*i )

Constructs a string list consisting of the single latin-1 string i.

QStringList QStringList::fromStrList ( constQStrList&ascii ) [static]

Converts from an ASCII-QStrList ascii to a QStringList (Unicode).

QStringList QStringList::grep ( constQString&str, boolcs = TRUE ) const

Returns a list of all strings containing the substring str.

If cs is TRUE, the grep is done case-sensitively; otherwise case is ignored.

QStringList QStringList::grep ( constQRegExp&expr ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a list of all the strings that contain a substring that matches the regular expression expr.

QString QStringList::join ( constQString&sep ) const

Joins the string list into a single string with each element separated by the string sep.

See also split().

void QStringList::sort ()

Sorts the list of strings in ascending case-sensitive order.

Sorting is very fast. It uses the Qt Template Library's efficient HeapSort implementation that has a time complexity of O(n*log n).

If you want to sort your strings in an arbitrary order consider using a QMap. For example you could use a QMap<QString,QString> to create a case-insensitive ordering (e.g. mapping the lowercase text to the text), or a QMap<int,QString> to sort the strings by some integer index, etc.

Example: themes/themes.cpp.

QStringList QStringList::split ( constQRegExp&sep, constQString&str, boolallowEmptyEntries = FALSE ) [static]

Splits the string str into strings wherever the regular expression sep occurs, and returns the list of those strings.

If allowEmptyEntries is TRUE, an empty string is inserted in the list wherever the separator matches twice without intervening text.

For example, if you split the string "a,,b,c" on commas, split() returns the three-item list "a", "b", "c" if allowEmptyEntries is FALSE (the default), and the four-item list "a", "", "b", "c" if allowEmptyEntries is TRUE.

If sep does not match anywhere in str, split() returns a list consisting of the single string str.

See also join() and QString::section().

Examples: chart/element.cpp, dirview/dirview.cpp and network/httpd/httpd.cpp.

QStringList QStringList::split ( constQString&sep, constQString&str, boolallowEmptyEntries = FALSE ) [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This version of the function uses a QString as separator, rather than a regular expression.

If sep is an empty string, the return value is a list of one-character strings: split( QString( "" ), "mfc" ) returns the three-item list, "m", "f", "c".

If allowEmptyEntries is TRUE, an empty string is inserted in the list wherever the separator matches twice without intervening text.

See also join() and QString::section().

QStringList QStringList::split ( constQChar&sep, constQString&str, boolallowEmptyEntries = FALSE ) [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This version of the function uses a QChar as separator, rather than a regular expression.

See also join() and QString::section().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文