QStringList 类

QStringList class provides a list of strings. 更多...

头: #include <QStringList>
qmake: QT += core
继承: QList

注意: 此类的所有函数 可重入 .

公共函数

QStringList ()
QStringList (const QString & str )
QStringList (const QList<QString> & other )
QStringList (QList<QString> && other )
QStringList (std::initializer_list<QString> args )
bool contains (const QString & str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringList filter (const QString & str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QStringList filter (const QRegExp & rx ) const
QStringList filter (const QRegularExpression & re ) const
int indexOf (const QRegExp & rx , int from = 0) const
int indexOf (QRegExp & rx , int from = 0) const
int indexOf (const QRegularExpression & re , int from = 0) const
QString join (const QString & separator ) const
QString join (QLatin1String separator ) const
QString join (QChar separator ) const
int lastIndexOf (const QRegExp & rx , int from = -1) const
int lastIndexOf (QRegExp & rx , int from = -1) const
int lastIndexOf (const QRegularExpression & re , int from = -1) const
int removeDuplicates ()
QStringList & replaceInStrings (const QString & before , const QString & after , Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringList & replaceInStrings (const QRegExp & rx , const QString & after )
QStringList & replaceInStrings (const QRegularExpression & re , const QString & after )
void sort (Qt::CaseSensitivity cs = Qt::CaseSensitive)
QStringList operator+ (const QStringList & other ) const
QStringList & operator<< (const QString & str )
QStringList & operator<< (const QStringList & other )
QStringList & operator<< (const QList<QString> & other )
QStringList & operator= (const QList<QString> & other )
QStringList & operator= (QList<QString> && other )
typedef QMutableStringListIterator
typedef QStringListIterator

额外继承成员

  • 3 static public members inherited from QList

详细描述

QStringList class provides a list of strings.

QStringList 继承自 QList < QString >。像 QList , QStringList is 隐式共享 . It provides fast index-based access as well as fast insertions and removals. Passing string lists as value parameters is both fast and safe.

All of QList 's functionality also applies to QStringList . For example, you can use isEmpty () to test whether the list is empty, and you can call functions like append (), prepend (), insert (), replace (), removeAll (), removeAt (), removeFirst (), removeLast (),和 removeOne () to modify a QStringList . In addition, QStringList provides a few convenience functions that make handling lists of strings easier:

添加字符串

Strings can be added to a list using the append() , operator+= () 和 operator<< () functions. For example:

    QStringList fonts;
    fonts << "Arial" << "Helvetica" << "Times" << "Courier";
					

遍历字符串

To iterate over a list, you can either use index positions or QList 's Java-style and STL-style iterator types:

索引:

    for (int i = 0; i < fonts.size(); ++i)
         cout << fonts.at(i).toLocal8Bit().constData() << endl;
					

Java 样式迭代器:

    QStringListIterator javaStyleIterator(fonts);
    while (javaStyleIterator.hasNext())
         cout << javaStyleIterator.next().toLocal8Bit().constData() << endl;
					

STL 样式迭代器:

    QStringList::const_iterator constIterator;
    for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd();
           ++constIterator)
        cout << (*constIterator).toLocal8Bit().constData() << endl;
					

QStringListIterator class is simply a type definition for QListIterator < QString >. QStringList 还提供 QMutableStringListIterator class which is a type definition for QMutableListIterator < QString >.

操纵字符串

QStringList provides several functions allowing you to manipulate the contents of a list. You can concatenate all the strings in a string list into a single string (with an optional separator) using the join () 函数。例如:

    QString str = fonts.join(", ");
     // str == "Arial, Helvetica, Times, Courier"
					

The argument to join can be a single character or a string.

To break up a string into a string list, use the QString::split () 函数:

    QStringList list;
    list = str.split(',');
     // list: ["Arial", "Helvetica", "Times", "Courier"]
					

The argument to split can be a single character, a string, a QRegularExpression or a (deprecated) QRegExp .

此外, operator+ () function allows you to concatenate two string lists into one. To sort a string list, use the sort () 函数。

QString 列表还提供 filter () function which lets you to extract a new list which contains only those strings which contain a particular substring (or match a particular regular expression):

    QStringList monospacedFonts = fonts.filter(QRegularExpression("Courier|Fixed"));
					

contains () function tells you whether the list contains a given string, while the indexOf () function returns the index of the first occurrence of the given string. The lastIndexOf () function on the other hand, returns the index of the last occurrence of the string.

Finally, the replaceInStrings () 函数调用 QString::replace () on each string in the string list in turn. For example:

    QStringList files;
    files << "$QTDIR/src/moc/moc.y"
          << "$QTDIR/src/moc/moc.l"
          << "$QTDIR/include/qconfig.h";
    files.replaceInStrings("$QTDIR", "/usr/lib/qt");
    // files: [ "/usr/lib/qt/src/moc/moc.y", ...]
					

另请参阅 QString .

成员函数文档编制

QStringList:: QStringList ()

Constructs an empty string list.

QStringList:: QStringList (const QString & str )

Constructs a string list that contains the given string, str . Longer lists are easily created like this:

    QStringList longerList = (QStringList() << str1 << str2 << str3);
					

另请参阅 append ().

QStringList:: QStringList (const QList < QString > & other )

构造副本为 other .

This operation takes 常量时间 ,因为 QStringList is 隐式共享 . This makes returning a QStringList from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes 线性时间 .

另请参阅 operator= ().

QStringList:: QStringList ( QList < QString > && other )

这是重载函数。

Move-constructs from QList < QString >.

After a successful construction, other will be empty.

该函数在 Qt 5.4 引入。

QStringList:: QStringList ( std::initializer_list < QString > args )

Construct a list from a std::initializer_list given by args .

This constructor is only enabled if the compiler supports C++11 initializer lists.

该函数在 Qt 4.8 引入。

bool QStringList:: contains (const QString & str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回 true 若列表包含字符串 str ;否则返回 false 。搜索不区分大小写若 cs is Qt::CaseInsensitive ;搜索区分大小写,默认情况下。

另请参阅 indexOf (), lastIndexOf (),和 QString::contains ().

QStringList QStringList:: filter (const QString & str , Qt::CaseSensitivity cs = Qt::CaseSensitive) const

返回所有字符串的列表包含子字符串 str .

cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.

    QStringList list;
    list << "Bill Murray" << "John Doe" << "Bill Clinton";
    QStringList result;
    result = list.filter("Bill");
    // result: ["Bill Murray", "Bill Clinton"]
					

这相当于

    QStringList result;
    foreach (const QString &str, list) {
        if (str.contains("Bill"))
            result += str;
    }
					

另请参阅 contains ().

QStringList QStringList:: filter (const QRegExp & rx ) const

这是重载函数。

Returns a list of all the strings that match the regular expression rx .

QStringList QStringList:: filter (const QRegularExpression & re ) const

这是重载函数。

Returns a list of all the strings that match the regular expression re .

该函数在 Qt 5.0 引入。

int QStringList:: indexOf (const QRegExp & rx , int from = 0) const

Returns the index position of the first exact match of rx in the list, searching forward from index position from . Returns -1 if no item matched.

By default, this function is case sensitive.

另请参阅 lastIndexOf (), contains (),和 QRegExp::exactMatch ().

int QStringList:: indexOf ( QRegExp & rx , int from = 0) const

此函数重载 indexOf ().

Returns the index position of the first exact match of rx in the list, searching forward from index position from . Returns -1 if no item matched.

By default, this function is case sensitive.

If an item matched, the rx regular expression will contain the matched objects (see QRegExp::matchedLength , QRegExp::cap ).

该函数在 Qt 4.5 引入。

另请参阅 lastIndexOf (), contains (),和 QRegExp::exactMatch ().

int QStringList:: indexOf (const QRegularExpression & re , int from = 0) const

这是重载函数。

Returns the index position of the first match of re in the list, searching forward from index position from . Returns -1 if no item matched.

该函数在 Qt 5.0 引入。

另请参阅 lastIndexOf ().

QString QStringList:: join (const QString & separator ) const

Joins all the string list's strings into a single string with each element separated by the given separator (which can be an empty string).

另请参阅 QString::split ().

QString QStringList:: join ( QLatin1String separator ) const

此函数重载 join ().

该函数在 Qt 5.8 引入。

QString QStringList:: join ( QChar separator ) const

此函数重载 join ().

该函数在 Qt 5.0 引入。

int QStringList:: lastIndexOf (const QRegExp & rx , int from = -1) const

Returns the index position of the last exact match of rx in the list, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

By default, this function is case sensitive.

另请参阅 indexOf (), contains (),和 QRegExp::exactMatch ().

int QStringList:: lastIndexOf ( QRegExp & rx , int from = -1) const

此函数重载 lastIndexOf ().

Returns the index position of the last exact match of rx in the list, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

By default, this function is case sensitive.

If an item matched, the rx regular expression will contain the matched objects (see QRegExp::matchedLength , QRegExp::cap ).

该函数在 Qt 4.5 引入。

另请参阅 indexOf (), contains (),和 QRegExp::exactMatch ().

int QStringList:: lastIndexOf (const QRegularExpression & re , int from = -1) const

这是重载函数。

Returns the index position of the last exact match of re in the list, searching backward from index position from 。若 from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

该函数在 Qt 5.0 引入。

另请参阅 indexOf ().

int QStringList:: removeDuplicates ()

This function removes duplicate entries from a list. The entries do not have to be sorted. They will retain their original order.

Returns the number of removed entries.

该函数在 Qt 4.5 引入。

QStringList &QStringList:: replaceInStrings (const QString & before , const QString & after , Qt::CaseSensitivity cs = Qt::CaseSensitive)

Returns a string list where every string has had the before text replaced with the after text wherever the before text is found. The before text is matched case-sensitively or not depending on the cs 标志。

例如:

    QStringList list;
    list << "alpha" << "beta" << "gamma" << "epsilon";
    list.replaceInStrings("a", "o");
    // list == ["olpho", "beto", "gommo", "epsilon"]
					

另请参阅 QString::replace ().

QStringList &QStringList:: replaceInStrings (const QRegExp & rx , const QString & after )

这是重载函数。

Replaces every occurrence of the regexp rx , in each of the string lists's strings, with after . Returns a reference to the string list.

例如:

    QStringList list;
    list << "alpha" << "beta" << "gamma" << "epsilon";
    list.replaceInStrings(QRegExp("^a"), "o");
    // list == ["olpha", "beta", "gamma", "epsilon"]
					

For regular expressions that contain capturing parentheses , occurrences of \1 , \2 , ..., in after are replaced with rx .cap(1), rx .cap(2), ...

例如:

    QStringList list;
    list << "Bill Clinton" << "Murray, Bill";
    list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1");
    // list == ["Bill Clinton", "Bill Murray"]
					

QStringList &QStringList:: replaceInStrings (const QRegularExpression & re , const QString & after )

这是重载函数。

Replaces every occurrence of the regular expression re , in each of the string lists's strings, with after . Returns a reference to the string list.

例如:

    QStringList list;
    list << "alpha" << "beta" << "gamma" << "epsilon";
    list.replaceInStrings(QRegularExpression("^a"), "o");
    // list == ["olpha", "beta", "gamma", "epsilon"]
					

For regular expressions that contain capturing groups, occurrences of \1 , \2 , ..., in after are replaced with the string captured by the corresponding capturing group.

例如:

    QStringList list;
    list << "Bill Clinton" << "Murray, Bill";
    list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1");
    // list == ["Bill Clinton", "Bill Murray"]
					

该函数在 Qt 5.0 引入。

void QStringList:: sort ( Qt::CaseSensitivity cs = Qt::CaseSensitive)

Sorts the list of strings in ascending order. If cs is Qt::CaseSensitive (the default), the string comparison is case sensitive; otherwise the comparison is case insensitive.

Sorting is performed using the STL's std::sort() algorithm, which averages linear-logarithmic time , i.e. O( n log n ).

If you want to sort your strings in an arbitrary order, consider using the QMap class. For example, you could use a QMap < QString , QString > to create a case-insensitive ordering (e.g. with the keys being lower-case versions of the strings, and the values being the strings), or a QMap <int, QString > to sort the strings by some integer index.

QStringList QStringList:: operator+ (const QStringList & other ) const

Returns a string list that is the concatenation of this string list with the other string list.

另请参阅 append ().

QStringList &QStringList:: operator<< (const QString & str )

Appends the given string, str , to this string list and returns a reference to the string list.

另请参阅 append ().

QStringList &QStringList:: operator<< (const QStringList & other )

这是重载函数。

追加 other string list to the string list and returns a reference to the latter string list.

QStringList &QStringList:: operator<< (const QList < QString > & other )

这是重载函数。

追加 other string list to the string list and returns a reference to the latter string list.

该函数在 Qt 5.4 引入。

QStringList &QStringList:: operator= (const QList < QString > & other )

Copy assignment operator from QList < QString >. Assigns the other list of strings to this string list.

After the operation, other and *this will be equal.

该函数在 Qt 5.4 引入。

QStringList &QStringList:: operator= ( QList < QString > && other )

这是重载函数。

Move assignment operator from QList < QString >. Moves the other list of strings to this string list.

After the operation, other will be empty.

该函数在 Qt 5.4 引入。

相关非成员

typedef QMutableStringListIterator

QStringListIterator type definition provides a Java-style non-const iterator for QStringList .

QStringList provides both Java 样式迭代器 and STL 样式迭代器 . The Java-style non-const iterator is simply a type definition for QMutableListIterator < QString >.

另请参阅 QStringListIterator and QStringList::iterator .

typedef QStringListIterator

The QStringListIterator type definition provides a Java-style const iterator for QStringList .

QStringList provides both Java 样式迭代器 and STL 样式迭代器 . The Java-style const iterator is simply a type definition for QListIterator < QString >.

另请参阅 QMutableStringListIterator and QStringList::const_iterator .