Manipulating the strings
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(",");
To break up a string into a string list, use the QString::split() function:
QStringList list;
list = str.split(",");
The argument to split can be a single character, a string, or a QRegExp.
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(QRegExp("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");
See also QString.
Member Function Documentation
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);
See also append().
QStringList::QStringList ( const QStringList & other )
Constructs a copy of the other string list.
This operation takes constant time because QStringList is implicitly shared, making the process of 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=().
QStringList::QStringList ( const QList<QString> & other )
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=().
bool QStringList::contains ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
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().
QStringList QStringList::filter ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) 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.
QStringList list;
list << "Bill Murray" << "John Doe" << "Bill Clinton";
QStringList result;
result = list.filter("Bill");
This is equivalent to
QStringList result;
foreach (QString str, list) {
if (str.contains("Bill"))
result += str;
}
See also contains().
QStringList QStringList::filter ( const QRegExp & rx ) const
This is an overloaded member function, provided for convenience.
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 lastIndexOf(), contains(), and 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 the given separator (which can be an empty string).
See also QString::split().
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 indexOf(), contains(), and QRegExp::exactMatch().
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 flag.
For example:
QStringList list;
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings("a", "o");
See also QString::replace().
QStringList & QStringList::replaceInStrings ( const QRegExp & rx, const QString & after )
This is an overloaded member function, provided for convenience.
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:
QStringList list;
list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings(QRegExp("^a"), "o");
For regular expressions that contain capturing parentheses, occurrences of \1, \2, ..., in after are replaced with rx.cap(1), rx.cap(2), ...
For example:
QStringList list;
list << "Bill Clinton" << "Murray, Bill";
list.replaceInStrings(QRegExp("^(.*), (.*)$"), "\\2 \\1");
void QStringList::sort ()
Sorts the list of strings in ascending order (case sensitively).
Sorting is performed using Qt's qSort() algorithm, which operates in 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.
See also qSort().
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 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 append().
QStringList & QStringList::operator<< ( const QStringList & other )
This is an overloaded member function, provided for convenience.
Appends the other string list to the string list and returns a reference to the latter string list.
Related Non-Members
typedef QMutableStringListIterator
The 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.
typedef QStringListIterator
The 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.
QDataStream & operator<< ( QDataStream & out, const QStringList & list )
This is an overloaded member function, provided for convenience.
Writes the given string list to the specified out stream.
See also Format of the QDataStream Operators.
This is an overloaded member function, provided for convenience.
Reads a string list from the given in stream into the specified list.
See also Format of the QDataStream Operators.
Member Function Documentation
Iterator QStringList::fromLast ()
Use end() instead.
For example, if you have code like
QStringList::Iterator i = list.fromLast();
you can rewrite it as
QStringList::Iterator i = list.isEmpty() ? list.end() : --list.end();
ConstIterator QStringList::fromLast () const
This is an overloaded member function, provided for convenience.
Use end() instead.
For example, if you have code like
QStringList::ConstIterator i = list.fromLast();
you can rewrite it as
QStringList::ConstIterator i = list.isEmpty() ? list.end() : --list.end();
QStringList QStringList::grep ( const QString & str, bool cs = true ) const
Use filter() instead.
QStringList QStringList::grep ( const QRegExp & rx ) const
This is an overloaded member function, provided for convenience.
Use filter() instead.
QStringList & QStringList::gres ( const QRegExp & rx, const QString & after )
Use replaceInStrings() instead.
QStringList & QStringList::gres ( const QString & before, const QString & after, bool cs = true )
This is an overloaded member function, provided for convenience.
Use replaceInStrings() instead.
QStringList QStringList::split ( const QRegExp & sep, const QString & str, bool allowEmptyEntries = 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.
Use split(QRegExp("\\s+"), str) to split on arbitrary amounts of whitespace.
If sep does not match anywhere in str, split() returns a single element list with the element containing the original string, str.
See also join() and QString::section().
QStringList QStringList::split ( const QChar & sep, const QString & str, bool allowEmptyEntries = false ) [static]
This is an overloaded member function, provided for convenience.
This version of the function uses a QChar as separator.
See also join() and QString::section().
QStringList QStringList::split ( const QString & sep, const QString & str, bool allowEmptyEntries = false ) [static]
This is an overloaded member function, provided for convenience.
This version of the function uses a QString as separator.
If sep is an empty string, the return value is a list of one-character strings: split(QString(""), "four") returns the four-item list, "f", "o", "u", "r".
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().