IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

QStringList Class

The https:// class provides a list of strings.

All functions in this class are https://.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

QStringList Class▲

  • Header: QStringList

  • qmake: QT += core

  • Group: QStringList is part of tools, https://, string-processing

Detailed Description▲

https:// inherits from https://<QString>. Like https://, https:// is https://. 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 https://'s functionality also applies to https://. For example, you can use https:// to test whether the list is empty, and you can call functions like append(), prepend(), insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne() to modify a https://. In addition, https:// provides a few convenience functions that make handling lists of strings easier:

Initializing▲

The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:

 
Sélectionnez
    QStringList fonts = { "Arial", "Helvetica", "Times" };

Adding Strings▲

Strings can be added to a list using the https://() https://(), https://() and https://() functions.

https://() can be used to conveniently add multiple elements to a list:

 
Sélectionnez
    fonts &lt;&lt; "Courier" &lt;&lt; "Verdana";

Iterating Over the Strings▲

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

Indexing:

 
Sélectionnez
    for (int i = 0; i &lt; fonts.size(); ++i)
         cout &lt;&lt; fonts.at(i).toLocal8Bit().constData() &lt;&lt; endl;

Java-style iterator:

 
Sélectionnez
    QStringListIterator javaStyleIterator(fonts);
    while (javaStyleIterator.hasNext())
         cout &lt;&lt; javaStyleIterator.next().toLocal8Bit().constData() &lt;&lt; endl;

STL-style iterator:

 
Sélectionnez
    QStringList::const_iterator constIterator;
    for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd();
           ++constIterator)
        cout &lt;&lt; (*constIterator).toLocal8Bit().constData() &lt;&lt; endl;

The https:// class is simply a type definition for QListIterator<QString>. https:// also provide the https:// class which is a type definition for QMutableListIterator<QString>.

Manipulating the Strings▲

https:// 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 https://() function. For example:

 
Sélectionnez
    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() function:

 
Sélectionnez
    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.

In addition, the https://() function allows you to concatenate two string lists into one. To sort a string list, use the https://() function.

QString list also provides the https://() 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):

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

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

Finally, the https://() function calls QString::replace() on each string in the string list in turn. For example:

 
Sélectionnez
    QStringList files;
    files &lt;&lt; "$QTDIR/src/moc/moc.y"
          &lt;&lt; "$QTDIR/src/moc/moc.l"
          &lt;&lt; "$QTDIR/include/qconfig.h";

    files.replaceInStrings("$QTDIR", "/usr/lib/qt");
    // files: [ "/usr/lib/qt/src/moc/moc.y", ...]

See Also▲

See also https://

Member Function Documentation▲

 

QStringList::QStringList()▲

Constructs an empty string list.

[default] QStringList::QStringList(const QString &str)▲

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

 
Sélectionnez
    QStringList longerList = (QStringList() &lt;&lt; str1 &lt;&lt; str2 &lt;&lt; str3);
See Also▲

See also append()

[default] QStringList::QStringList(const QList<QString> &other)▲

Constructs a copy of other.

This operation takes https://, because https:// is https://. This makes returning a https:// from a function very fast. If a shared instance is modified, it will be copied (copy-on-write), and that takes https://.

See Also▲

See also https://()

[default, since 5.4] QStringList::QStringList(QList<QString> &&other)▲

This is an overloaded function.

Move-constructs from https://<QString>.

After a successful construction, other will be empty.

This function was introduced in Qt 5.4.

[default, since 4.8] 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.

This function was introduced in Qt 4.8.

[since 5.10] bool QStringList::contains(QLatin1String str, int cs = ...) const▲

This is an overloaded function.

Returns true if the list contains the string str; otherwise returns false. The search is case insensitive if cs is Qt::CaseInsensitive; the search is case sensitive by default.

This function was introduced in Qt 5.10.

See Also▲

See also https://(), https://(), QString::contains()

[since 5.12] bool QStringList::contains(QStringView str, int cs = ...) const▲

This is an overloaded function.

Returns true if the list contains the string str; otherwise returns false. The search is case insensitive if cs is Qt::CaseInsensitive; the search is case sensitive by default.

This function was introduced in Qt 5.12.

QStringList QStringList::filter(const QString &str, int cs = ...) const▲

Returns a list of all the strings containing the substring str.

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

 
Sélectionnez
    QStringList list;
    list &lt;&lt; "Bill Murray" &lt;&lt; "John Doe" &lt;&lt; "Bill Clinton";

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

This is equivalent to

 
Sélectionnez
    QStringList result;
    foreach (const QString &amp;str, list) {
        if (str.contains("Bill"))
            result += str;
    }
See Also▲

See also https://()

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

This is an overloaded function.

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

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.

See Also▲

See also https://(), https://(), QRegExp::exactMatch()

[since 5.13] int QStringList::indexOf(QStringView str, int from = 0) const▲

This is an overloaded function.

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

This function was introduced in Qt 5.13.

See Also▲

See also https://(), https://()

[since 5.13] int QStringList::indexOf(QLatin1String str, int from = 0) const▲

This is an overloaded function.

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

This function was introduced in Qt 5.13.

See Also▲

See also https://(), https://()

[since 4.5] int QStringList::indexOf(QRegExp &rx, int from = 0) const▲

This function overloads https://().

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.

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

This function was introduced in Qt 4.5.

See Also▲

See also https://(), https://(), QRegExp::exactMatch()

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).

See Also▲

See also QString::split()

[since 5.8] QString QStringList::join(QLatin1String separator) const▲

This function overloads https://().

This function was introduced in Qt 5.8.

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. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

See Also▲

See also https://(), https://(), QRegExp::exactMatch()

[since 5.13] int QStringList::lastIndexOf(QStringView str, int from = -1) const▲

This is an overloaded function.

Returns the index position of the last occurrence of str in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

This function was introduced in Qt 5.13.

See Also▲

See also https://(), https://()

[since 5.13] int QStringList::lastIndexOf(QLatin1String str, int from = -1) const▲

This is an overloaded function.

Returns the index position of the last occurrence of str in the list, searching backward from index position from. If from is -1 (the default), the search starts at the last item. Returns -1 if no item matched.

This function was introduced in Qt 5.13.

See Also▲

See also https://(), https://()

[since 4.5] int QStringList::lastIndexOf(QRegExp &rx, int from = -1) const▲

This function overloads https://().

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

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

This function was introduced in Qt 4.5.

See Also▲

See also https://(), https://(), QRegExp::exactMatch()

[since 4.5] 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.

This function was introduced in Qt 4.5.

QStringList &QStringList::replaceInStrings(const QString &before, const QString &after, int cs = ...)▲

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 flag.

For example:

 
Sélectionnez
    QStringList list;
    list &lt;&lt; "alpha" &lt;&lt; "beta" &lt;&lt; "gamma" &lt;&lt; "epsilon";
    list.replaceInStrings("a", "o");
    // list == ["olpho", "beto", "gommo", "epsilon"]
See Also▲

See also QString::replace()

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

This is an overloaded function.

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

For example:

 
Sélectionnez
    QStringList list;
    list &lt;&lt; "alpha" &lt;&lt; "beta" &lt;&lt; "gamma" &lt;&lt; "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), ...

For example:

 
Sélectionnez
    QStringList list;
    list &lt;&lt; "Bill Clinton" &lt;&lt; "Murray, Bill";
    list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1");
    // list == ["Bill Clinton", "Bill Murray"]

void QStringList::sort(int cs = ...)▲

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 https://, i.e. O(n log n).

If you want to sort your strings in an arbitrary order, consider using the https:// class. For example, you could use a https://<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 https://<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.

See Also▲

See also append()

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

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

See Also▲

See also append()

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

This is an overloaded function.

Appends the other string list to the string list and returns a reference to the latter string list.

[since 5.4] QStringList &QStringList::operator<<(const QList<QString> &other)▲

This is an overloaded function.

Appends the other string list to the string list and returns a reference to the latter string list.

This function was introduced in Qt 5.4.

[default, since 5.4] QStringList &QStringList::operator=(const QList<QString> &other)▲

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

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

This function was introduced in Qt 5.4.

[default, since 5.4] QStringList &QStringList::operator=(QList<QString> &&other)▲

This is an overloaded function.

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

After the operation, other will be empty.

This function was introduced in Qt 5.4.

Related Non-Members▲

 

QMutableStringListIterator▲

The https:// type definition provides a Java-style non-const iterator for https://.

https:// provides both https:// and https://. The Java-style non-const iterator is simply a type definition for QMutableListIterator<QString>.

See Also▲

See also https://, QStringList::iterator

QStringListIterator▲

The QStringListIterator type definition provides a Java-style const iterator for https://.

https:// provides both https:// and https://. The Java-style const iterator is simply a type definition for QListIterator<QString>.

See Also▲

See also https://, QStringList::const_iterator

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+   

Ce document est disponible sous la licence https:// telle que publiée par la Free Software Foundation.