CopperSpice API  1.7.2
QStringList Class Reference

Provides a container which is optimized for strings. More...

Inheritance diagram for QStringList:
QList< QString >

Public Methods

 QStringList ()
 
 QStringList (const QList< QString > &other)
 
 QStringList (const QString &value)
 
 QStringList (const QStringList &other)
 
 QStringList (std::initializer_list< QString > args)
 
bool contains (const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
QStringList filter (const QRegularExpression8 &rx) const
 
QStringList filter (const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
 
int indexOf (const QRegularExpression8 &rx, int from=0) const
 
int indexOf (QRegularExpression8 &rx, int from=0) const
 
QString join (const QString &separator) const
 
int lastIndexOf (const QRegularExpression8 &rx, int from=-1) const
 
int lastIndexOf (QRegularExpression8 &rx, int from=-1) const
 
QStringList operator+ (const QStringList &other) const
 
QStringList & operator<< (const QString &str)
 
QStringList & operator<< (const QStringList &other)
 
QStringList & operator= (const QStringList &other)
 
int removeDuplicates ()
 
QStringList & replaceInStrings (const QRegularExpression8 &rx, const QString &after)
 
QStringList & replaceInStrings (const QString &before, const QString &after, Qt::CaseSensitivity cs=Qt::CaseSensitive)
 
void sort ()
 
- Public Methods inherited from QList< QString >
 QList () = default
 
 QList (const QList< QString > &other) = default
 
 QList (Input_Iterator first, Input_Iterator last)
 
 QList (QList< QString > &&other) = default
 
 QList (std::initializer_list< QString > args)
 
 ~QList () = default
 
void append (const QList< QString > &other)
 
void append (const QString &value)
 
void append (QList< QString > &&other)
 
void append (QString &&value)
 
const QStringat (size_type i) const
 
QStringback ()
 
const QStringback () const
 
iterator begin ()
 
const_iterator begin () const
 
const_iterator cbegin () const
 
const_iterator cend () const
 
void clear ()
 
const_iterator constBegin () const
 
const_iterator constEnd () const
 
const_reference constFirst () const
 
const_reference constLast () const
 
bool contains (const QString &value) const
 
size_type count () const
 
size_type count (const QString &value) const
 
const_reverse_iterator crbegin () const
 
const_reverse_iterator crend () const
 
bool empty () const
 
iterator end ()
 
const_iterator end () const
 
bool endsWith (const QString &value) const
 
iterator erase (const_iterator begin, const_iterator end)
 
iterator erase (const_iterator pos)
 
QStringfirst ()
 
const QStringfirst () const
 
QStringfront ()
 
const QStringfront () const
 
size_type indexOf (const QString &value, size_type from=0) const
 
iterator insert (iterator before, const QString &value)
 
void insert (size_type i, const QString &value)
 
bool isEmpty () const
 
QStringlast ()
 
const QStringlast () const
 
size_type lastIndexOf (const QString &value, size_type from=-1) const
 
size_type length () const
 
QList< QStringmid (size_type pos, size_type length=-1) const
 
void move (size_type from, size_type to)
 
bool operator!= (const QList< QString > &other) const
 
QList< QStringoperator+ (const QList< QString > &other) const
 
QList< QString > & operator+= (const QList< QString > &other)
 
QList< QString > & operator+= (const QString &value)
 
QList< QString > & operator<< (const QList< QString > &other)
 
QList< QString > & operator<< (const QString &value)
 
QList< QString > & operator= (const QList< QString > &other) = default
 
QList< QString > & operator= (QList< QString > &&other) = default
 
bool operator== (const QList< QString > &other) const
 
QStringoperator[] (size_type i)
 
const QStringoperator[] (size_type i) const
 
void pop_back ()
 
void pop_front ()
 
void prepend (const QString &value)
 
void push_back (const QString &value)
 
void push_front (const QString &value)
 
reverse_iterator rbegin ()
 
const_reverse_iterator rbegin () const
 
size_type removeAll (const QString &value)
 
void removeAt (size_type i)
 
void removeFirst ()
 
void removeLast ()
 
bool removeOne (const QString &value)
 
reverse_iterator rend ()
 
const_reverse_iterator rend () const
 
void replace (size_type i, const QString &value)
 
void resize (size_type size)
 
size_type size () const
 
bool startsWith (const QString &value) const
 
void swap (QList< QString > &other)
 
void swap (size_type i, size_type j)
 
QString takeAt (size_type i)
 
QString takeFirst ()
 
QString takeLast ()
 
QSet< QStringtoSet () const
 
std::list< QStringtoStdList () const
 
QVector< QStringtoVector () const
 
QString value (size_type i) const
 
QString value (size_type i, const QString &defaultValue) const
 

Related Functions

These are not member functions

QDataStreamoperator<< (QDataStream &out, const QStringList &list)
 
QDataStreamoperator>> (QDataStream &in, QStringList &list)
 
typedef QMutableStringListIterator
 
typedef QStringListIterator
 

Additional Inherited Members

- Public Typedefs inherited from QList< QString >
using allocator_type = typename std::deque< QString >::allocator_type
 
using const_iterator = typename std::deque< QString >::const_iterator
 
using const_pointer = typename std::deque< QString >::const_pointer
 
using const_reference = typename std::deque< QString >::const_reference
 
using const_reverse_iterator = typename std::deque< QString >::const_reverse_iterator
 
using difference_type = typename std::deque< QString >::difference_type
 
using iterator = typename std::deque< QString >::iterator
 
using Java_Iterator = QListIterator< QString >
 
using Java_MutableIterator = QMutableListIterator< QString >
 
using pointer = typename std::deque< QString >::pointer
 
using reference = typename std::deque< QString >::reference
 
using reverse_iterator = typename std::deque< QString >::reverse_iterator
 
using size_type = typename std::deque< QString >::difference_type
 
using value_type = typename std::deque< QString >::value_type
 
- Static Public Methods inherited from QList< QString >
static QList< QStringfromSet (const QSet< QString > &set)
 
static QList< QStringfromStdList (const std::list< QString > &other)
 
static QList< QStringfromVector (const QVector< QString > &vector)
 

Detailed Description

The QStringList class provides a container which is optimized for strings. This container stores a list of strings using a QList<QString>. QStringList provides fast index access as well as fast insertions and removals at both ends.

For an overview and comparison of all containers, refer to the documentation for Container Classes. Refer to the section on Time Complexity for a discussion about which operations will be relatively faster or slower for a given container with a size of n.

Basic Operations

Since QStringList inherits from QList all of the methods in QList are available in this class. For example, isEmpty() can be used to determine if the string list has any elements.

QStringList also provides methods which make handling lists of strings easier. Methods like append(), prepend(), insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne() can be used to modify a QStringList.

Example Code

In addition to append(), the following code shows how to add elements using the operator<<() method.

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

To concatenate all of the strings in an existing string list into a single string, with an optional separator, use join().

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

To create a QStringList from a QString using a separator character, the QString::split() method is called.

QString str = "apple, pear, orange, plum";
list = str.split(", "); // list: ["apple", "pear", "orange", "plum"]

Iterators

To iterate over a QStringList you can use Java style or STL style iterators.

// Java style iterator
QStringListIterator javaStyleIterator(fonts);
while (javaStyleIterator.hasNext()) {
cout << javaStyleIterator.next().toUtf8().constData() << endl;
}
// STL style iterator
for (iter = fonts.constBegin(); iter != fonts.constEnd(); ++iter) {
cout << (*iter).toUtf8().constData() << endl;
}
See also
QList, QString

Constructor & Destructor Documentation

QStringList::QStringList ( )
inline

Constructs an empty string list.

QStringList::QStringList ( const QString value)
inlineexplicit

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

Constructs a copy of the other string list.

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

Constructs a copy of other.

See also
operator=()
QStringList::QStringList ( std::initializer_list< QString args)
inline

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

Method Documentation

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(), QString8::contains()
QStringList QStringList::filter ( const QRegularExpression8 rx) const

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

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.

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

This is equivalent to

QStringList result;
for (const QString &str : list) {
if (str.contains("Bill")) {
result += str;
}
}
See also
contains()
int QStringList::indexOf ( const QRegularExpression8 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 method is case sensitive.

See also
lastIndexOf(), contains(), QPatternOption::ExactMatchOption
int QStringList::indexOf ( QRegularExpression8 rx,
int  from = 0 
) const

If an item matched the rx regular expression will contain the matched objects. 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 method is case sensitive.

See also
lastIndexOf(), contains(), QPatternOption::ExactMatchOption
QString QStringList::join ( const QString separator) const

Joins all the strings in the list into a single string with each element separated by the given separator. The separator can be empty.

See also
QString8::split()
int QStringList::lastIndexOf ( const QRegularExpression8 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.

By default this method is case sensitive.

See also
indexOf(), contains(), QPatternOption::ExactMatchOption
int QStringList::lastIndexOf ( QRegularExpression8 rx,
int  from = -1 
) const

If an item matched the rx regular expression will contain the matched objects. 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.

By default this method is case sensitive.

See also
indexOf(), contains(), QPatternOption::ExactMatchOption
QStringList QStringList::operator+ ( const QStringList &  other) const
inline

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

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

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

QStringList & QStringList::operator= ( const QStringList &  other)
inline

Copy assigns from other and returns a reference to this object.

int QStringList::removeDuplicates ( )

This method removes duplicate entries from a list. The entries do not have to be sorted and they will retain their original order. Returns the number of removed entries.

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

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

list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings(QRegularExpression8("^a"), "o"); // ["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), ...

list << "Bill Clinton" << "Murray, Bill";
list.replaceInStrings(QRegularExpression8("^(.*), (.*)$"), "\2 \1"); // list == ["Bill Clinton", "Bill Murray"]
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.

list << "alpha" << "beta" << "gamma" << "epsilon";
list.replaceInStrings("a", "o"); // ["olpho", "beto", "gommo", "epsilon"]
See also
QString8::replace()
void QStringList::sort ( )

Sorts the list of strings in ascending order, case sensitive.

Sorting is performed using the CopperSpice 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()

Friends And Related Function Documentation

QDataStream & operator<< ( QDataStream out,
const QStringList &  list 
)
related

Writes the given string list to the specified out stream.

Refer to Serializing Data Types for additional information.

QDataStream & operator>> ( QDataStream in,
QStringList &  list 
)
related

Reads a string list from the given in stream into the specified list.

Refer to Serializing Data Types for additional information.

typedef QMutableStringListIterator ( )
related

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, QStringList::iterator
typedef QStringListIterator ( )
related

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, QStringList::const_iterator