6.0.0-rc4Source

QStringList Struct

The QStringList class provides a list of strings. More…

Header#include <QStringList>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QStringjoin(const QString &sep) const
QStringjoin(QChar sep) const
boolcontains(const QString &s, Qt::CaseSensitivity cs) const
qsizetypeindexOf(const QString &s, qsizetype from, Qt::CaseSensitivity cs) const
QList<QString>filter(const QString &needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QList<QString> &replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
qsizetypelastIndexOf(const QString &s, qsizetype from, Qt::CaseSensitivity cs) const
QStringjoin(S sep) const
boolcontains(S s, Qt::CaseSensitivity cs) const
qsizetypeindexOf(S s, qsizetype from, Qt::CaseSensitivity cs) const
qsizetypelastIndexOf(S s, qsizetype from, Qt::CaseSensitivity cs) const
QList<QString>filter(S needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
QList<QString> &replaceInStrings(const B &before, const A &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)
QList<QString>filter(const QStringMatcher &matcher) const
QList<QString>filter(const QRegularExpression &re) const
qsizetypeindexOf(const QRegularExpression &re, qsizetype from = 0) const
qsizetypelastIndexOf(const QRegularExpression &re, qsizetype from = -1) const
QList<QString> &replaceInStrings(const QRegularExpression &re, const QString &after)
qsizetyperemoveDuplicates()
voidsort(Qt::CaseSensitivity cs = Qt::CaseSensitive)

Detailed Description

QStringList is a QList<QString> with convenience functions for working with strings: join(), filter(), replaceInStrings(), removeDuplicates() and sort(). Lists are usually produced by QString::split() and combined back with join():

QStringList fields = QString("a,b,c").split(',');
fields << "d";
QString csv = fields.join(',');   // "a,b,c,d"

QStringList is another name of QList<QString>, so everything that QList has is there too, and a function that takes one takes the other.

contains(), indexOf() and lastIndexOf() are those of QList when they are called without a case sensitivity, and compare case sensitively; the functions here are the ones that take a Qt::CaseSensitivity.

See also QString and QList.

Differences from Qt

The QStringView and QLatin1StringView overloads of join(), contains(), indexOf(), lastIndexOf(), filter() and replaceInStrings() are function templates that accept exactly those two types, so that a string literal selects the QString overload; they convert the view to a QString once per call.

Member Function Documentation

bool QStringList::contains(const QString &s, Qt::CaseSensitivity cs) const

Returns true if the list contains the string s, using the case sensitivity cs; otherwise returns false.

See also indexOf().

template <typename S, if_view<S> = 0>bool QStringList::contains(S s, Qt::CaseSensitivity cs) const

This is an overloaded function.

Returns true if the list contains the string s, a QStringView or a QLatin1StringView, using the case sensitivity cs.

This function was introduced in 5.12.

QList<QString> QStringList::filter(const QString &needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

Returns a list of the strings that contain the substring needle, using the case sensitivity cs.

See also QString::contains().

template <typename S, if_view<S> = 0>QList<QString> QStringList::filter(S needle, Qt::CaseSensitivity cs = Qt::CaseSensitive) const

This is an overloaded function.

Returns the strings that contain needle, a QStringView or a QLatin1StringView, using the case sensitivity cs.

This function was introduced in 5.14.

QList<QString> QStringList::filter(const QStringMatcher &matcher) const

This is an overloaded function.

Returns the strings in which matcher finds its pattern, with the case sensitivity of the matcher. This is faster than the QString overload for a long list, because the matcher prepares the pattern once.

This function is defined in <QStringMatcher>.

This function was introduced in 6.7.

QList<QString> QStringList::filter(const QRegularExpression &re) const

Returns the strings of the list that re matches somewhere.

This function was introduced in 5.0.

See also QString::contains().

qsizetype QStringList::indexOf(const QString &s, qsizetype from, Qt::CaseSensitivity cs) const

Returns the index position of the first string in the list equal to s, searching forward from index position from, or -1 if there is none. cs selects the case sensitivity.

See also contains().

template <typename S, if_view<S> = 0>qsizetype QStringList::indexOf(S s, qsizetype from, Qt::CaseSensitivity cs) const

This is an overloaded function.

Returns the index of the first string equal to s, a QStringView or a QLatin1StringView, searching forward from from and using the case sensitivity cs, or -1.

This function was introduced in 5.13.

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

This is an overloaded function.

Returns the index of the first string in the list, searching forward from from, that re matches exactly (the whole string), or -1.

This function was introduced in 5.0.

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

Returns the strings in the list joined into one string, with sep inserted between consecutive strings. An empty list gives an empty string.

See also QString::split().

QString QStringList::join(QChar sep) const

This is an overloaded function.

Joins the strings in the list using the character sep as the separator.

template <typename S, if_view<S> = 0>QString QStringList::join(S sep) const

This is an overloaded function.

Joins the strings with the separator sep, a QStringView or a QLatin1StringView.

This function was introduced in 5.14.

qsizetype QStringList::lastIndexOf(const QString &s, qsizetype from, Qt::CaseSensitivity cs) const

Returns the index of the last string equal to s, searching backward from index position from and using the case sensitivity cs, or -1 if there is none. A negative from counts from the end of the list, so the default of -1 starts at the last string.

This function was introduced in 6.7.

See also indexOf() and contains().

template <typename S, if_view<S> = 0>qsizetype QStringList::lastIndexOf(S s, qsizetype from, Qt::CaseSensitivity cs) const

This is an overloaded function.

Returns the index of the last string equal to s, a QStringView or a QLatin1StringView, searching backward from from and using the case sensitivity cs, or -1.

This function was introduced in 5.13.

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

This is an overloaded function.

Returns the index of the last string in the list, searching backward from from, that re matches exactly, or -1. A negative from counts from the end of the list.

This function was introduced in 5.0.

qsizetype QStringList::removeDuplicates()

Removes duplicate strings from the list, keeping the first occurrence of each, and returns the number of strings removed. Strings are compared case sensitively.

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

Replaces every occurrence of the string before with after in each string of the list, using the case sensitivity cs, and returns a reference to the list.

See also QString::replace().

template <typename B, typename A, if_views<B, A> = 0>QList<QString> & QStringList::replaceInStrings(const B &before, const A &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)

This is an overloaded function.

Replaces every occurrence of before with after in each string, using the case sensitivity cs. At least one of before and after is a QStringView and the other may be a QString.

This function was introduced in 5.14.

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

This is an overloaded function.

Replaces every match of re with after in each string of the list, as QString::replace() does, and returns a reference to the list.

This function was introduced in 5.0.

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

Sorts the list in ascending order, comparing strings with QString::compare() and the case sensitivity cs. The sort is not guaranteed to be stable.