The QStringList class provides a list of strings. More...
Header: | #include <QStringList> |
CMake: | find_package(Qt6 REQUIRED COMPONENTS Core) target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
Inherits: | QList |
Note: All functions in this class are reentrant.
QStringList(QList<QString> &&other) | |
QStringList(const QList<QString> &other) | |
QStringList(const QString &str) | |
QStringList & | operator=(QList<QString> &&other) |
QStringList & | operator=(const QList<QString> &other) |
bool | contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QLatin1StringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
bool | contains(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringList | filter(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringList | filter(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const |
QStringList | filter(const QRegularExpression &re) const |
qsizetype | indexOf(const QRegularExpression &re, qsizetype from = 0) const |
QString | join(const QString &separator) const |
QString | join(QStringView separator) const |
QString | join(QLatin1StringView separator) const |
QString | join(QChar separator) const |
qsizetype | lastIndexOf(const QRegularExpression &re, qsizetype from = -1) const |
qsizetype | removeDuplicates() |
QStringList & | replaceInStrings(const QString &before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
QStringList & | replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
QStringList & | replaceInStrings(const QString &before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
QStringList & | replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive) |
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 inherits from QList<QString>. Like QList, QStringList is implicitly shared. 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(), and removeOne() to modify a QStringList. In addition, QStringList provides a few convenience functions that make handling lists of strings easier:
The default constructor creates an empty list. You can use the initializer-list constructor to create a list with elements:
QStringList fonts = { "Arial", "Helvetica", "Times" };
Strings can be added to a list using the insert(), append(), operator+=() and operator<<() functions.
operator<<() can be used to conveniently add multiple elements to a list:
fonts << "Courier" << "Verdana";
To iterate over a list, you can either use index positions or QList's Java-style and STL-style iterator types:
Indexing:
for (int i = 0; i < fonts.size(); ++i) cout << fonts.at(i).toLocal8Bit().constData() << Qt::endl;
Java-style iterator:
QStringListIterator javaStyleIterator(fonts); while (javaStyleIterator.hasNext()) cout << javaStyleIterator.next().toLocal8Bit().constData() << Qt::endl;
STL-style iterator:
QStringList::const_iterator constIterator; for (constIterator = fonts.constBegin(); constIterator != fonts.constEnd(); ++constIterator) cout << (*constIterator).toLocal8Bit().constData() << Qt::endl;
The QStringListIterator class is simply a type definition for QListIterator<QString>. QStringList also provide the 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() function. For example:
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:
QStringList list; list = str.split(','); // list: ["Arial", "Helvetica", "Times", "Courier"]
The argument to split can be a single character, a string or a QRegularExpression.
In addition, the operator+() function allows you to concatenate two string lists into one. To sort a string list, use the sort() function.
QString list also provides the 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"));
The 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() function calls 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", ...]
See also QString.
[since 5.4]
QStringList::QStringList(QList<QString>
&&other)This is an overloaded function.
Move-constructs from QList<QString>.
After a successful construction, other will be empty.
This function was introduced in Qt 5.4.
Constructs a copy of other.
This operation takes constant time, because QStringList is implicitly shared. 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 linear time.
See also operator=().
Constructs a string list that contains the given string, str. Longer lists are easily created like this:
QStringList longerList = (QStringList() << str1 << str2 << str3);
See also append().
[since 5.4]
QStringList &QStringList::operator=(QList<QString> &&other)This is an overloaded function.
Move assignment operator from QList<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.
[since 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.
This function was introduced in Qt 5.4.
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.
See also indexOf(), lastIndexOf(), and QString::contains().
[since 5.10]
bool QStringList::contains(QLatin1StringView
str, Qt::CaseSensitivity cs = Qt::CaseSensitive) constThis 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 indexOf(), lastIndexOf(), and QString::contains().
[since 5.12]
bool QStringList::contains(QStringView str,
Qt::CaseSensitivity cs = Qt::CaseSensitive) constThis 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.
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.
QStringList list; list << "Bill Murray" << "John Doe" << "Bill Clinton"; QStringList result; result = list.filter("Bill"); // result: ["Bill Murray", "Bill Clinton"]
This is equivalent to
QStringList result; foreach (const QString &str, list) { if (str.contains("Bill")) result += str; }
See also contains().
[since 5.14]
QStringList QStringList::filter(QStringView str, Qt::CaseSensitivity cs = Qt::CaseSensitive) constThis is an overloaded function.
This function was introduced in Qt 5.14.
[since 5.0]
QStringList QStringList::filter(const QRegularExpression &re) constThis is an overloaded function.
Returns a list of all the strings that match the regular expression re.
This function was introduced in Qt 5.0.
[since 5.0]
qsizetype QStringList::indexOf(const QRegularExpression &re, qsizetype from = 0) constThis is an overloaded function.
Returns the index position of the first exact match of re in the list, searching forward from index position from. Returns -1 if no item matched.
This function was introduced in Qt 5.0.
See also lastIndexOf().
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 QString::split().
[since 5.14]
QString QStringList::join(QStringView separator) constThis is an overloaded function.
This function was introduced in Qt 5.14.
[since 5.8]
QString QStringList::join(QLatin1StringView separator) constThis function overloads join().
This function was introduced in Qt 5.8.
[since 5.0]
QString QStringList::join(QChar
separator) constThis function overloads join().
This function was introduced in Qt 5.0.
[since 5.0]
qsizetype QStringList::lastIndexOf(const QRegularExpression &re, qsizetype from = -1) constThis is an overloaded function.
Returns the index position of the last exact match of re 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.0.
See also indexOf().
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.
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:
QStringList list; list << "alpha" << "beta" << "gamma" << "epsilon"; list.replaceInStrings("a", "o"); // list == ["olpho", "beto", "gommo", "epsilon"]
See also QString::replace().
[since 5.14]
QStringList &QStringList::replaceInStrings(QStringView before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive)This is an overloaded function.
This function was introduced in Qt 5.14.
[since 5.14]
QStringList &QStringList::replaceInStrings(const
QString &before, QStringView after, Qt::CaseSensitivity cs = Qt::CaseSensitive)This is an overloaded function.
This function was introduced in Qt 5.14.
[since 5.14]
QStringList &QStringList::replaceInStrings(QStringView before, const QString &after, Qt::CaseSensitivity cs = Qt::CaseSensitive)This is an overloaded function.
This function was introduced in Qt 5.14.
[since 5.0]
QStringList &QStringList::replaceInStrings(const
QRegularExpression &re, const QString &after)This is an overloaded function.
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.
For example:
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.
For example:
QStringList list; list << "Bill Clinton" << "Murray, Bill"; list.replaceInStrings(QRegularExpression("^(.*), (.*)$"), "\\2 \\1"); // list == ["Bill Clinton", "Bill Murray"]
This function was introduced in Qt 5.0.
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.
Returns a string list that is the concatenation of this string list with the other string list.
See also append().
Appends the given string, str, to this string list and returns a reference to the string list.
See also append().
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.
[alias]
QMutableStringListIteratorThe QStringListIterator type definition provides a Java-style non-const iterator for QStringList.
QStringList provides both Java-style iterators and STL-style iterators. The Java-style non-const iterator is simply a type definition for QMutableListIterator<QString>.
See also QStringListIterator and QStringList::iterator.
[alias]
QStringListIteratorThe QStringListIterator type definition provides a Java-style const iterator for QStringList.
QStringList provides both Java-style iterators and STL-style iterators. The Java-style const iterator is simply a type definition for QListIterator<QString>.
See also QMutableStringListIterator and QStringList::const_iterator.