CopperSpice API  2.1.0
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
 
QString join (const QString &separator) const
 
int lastIndexOf (const 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 &stream, const QStringList &list)
 
QDataStreamoperator>> (QDataStream &stream, QStringList &list)
 
 QMutableStringListIterator
 
 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 and Documentation

Since QStringList inherits from QList<QString> all of the existing methods in QList are available in this class.

The method isEmpty() is used to determine if the current QStringList has any elements. This method is not documented in QStringList, it is documented in the QList class. As another example, methods like indexOf() or lastIndexOf() have additional overloads and their documentation can also be found in the QList class.

The QStringList class provides a vast number of other methods which make handling a list of strings easier. Methods like append(), prepend(), insert(), replace(), removeAll(), removeAt(), removeFirst(), removeLast(), and removeOne() can be used to modify or query the elements of a QStringList.

Example

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

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

To create a quoted string using the elements of a existing QStringList use join(). An optional separator can be specified as a way to indicate a delimiter between each element.

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

QStringList::QStringList ( const QString value)
inlineexplicit

Constructs a new QStringList which contains the given string str.

QStringList strList("apple");
See also
append()
QStringList::QStringList ( const QStringList &  other)
inline

Copy constructs a new QStringList from other.

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

Copy constructs a new QStringList from other.

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

Constructs a new QStringList using an std::initializer_list specified by args.

QStringList strList = {"apple", "orange"};

Method Documentation

bool QStringList::contains ( const QString str,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
) const

Returns true if the QStringList contains the string str, otherwise returns false. If cs is Qt::CaseSensitive, the string comparison is case sensitive, otherwise the comparison is case insensitive.

See also
indexOf(), lastIndexOf(), QString8::contains()
QStringList QStringList::filter ( const QRegularExpression8 rx) const

Returns a list of all the strings which 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 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 the following example.

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 is found.

By default this method is case sensitive.

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

Concatenates the strings in the QStringList into a single QString. Each element is delimited 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 the value from is -1, the search starts at the last element. Returns -1 if no item is found.

By default this method is case sensitive.

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

Returns a QStringList which is the concatenation of this QStringList and other.

See also
append()
QStringList & QStringList::operator<< ( const QString str)
inline

Appends the given str to this QStringList and returns a reference to this QStringList.

See also
append()
QStringList & QStringList::operator<< ( const QStringList &  other)
inline

Appends other to this QStringList and returns a reference to this QStringList.

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

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

int QStringList::removeDuplicates ( )

Removes duplicate elements from the current QStringList. The elements do not need to be sorted and they will retain their original order. Returns the number of removed elements.

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

Replaces every occurrence of the regular expression rx with after.

list << "apple" << "grape" << "orange" << "watermelon";
list.replaceInStrings(QRegularExpression8("e$"), "XX"); // ["applXX", "grapXX", "orangXX", "watermelon"]

For regular expressions which contain capturing parentheses, occurrences of \1, \2, etc, The after string is replaced with rx.cap(1), rx.cap(2), etc.

list << "apple" << "grape" << "orange" << "watermelon";
list.replaceInStrings(QRegularExpression8("([ao])"), "\\1\\1"); // ["aapple", "graape", "ooraange", "waatermeloon"]
QStringList & QStringList::replaceInStrings ( const QString before,
const QString after,
Qt::CaseSensitivity  cs = Qt::CaseSensitive 
)

Replaces the string before with the string after for every match in any given element. If cs is Qt::CaseSensitive the string comparison is case sensitive, otherwise the comparison is case insensitive.

list << "apple" << "grape" << "orange" << "watermelon";
list.replaceInStrings("ap", "XX"); // ["XXple", "grXXe", "orange", "watermelon"]
See also
QString8::replace()
void QStringList::sort ( )

Sorts the strings in the current QStringList in ascending order and case sensitive.

Friends And Related Function Documentation

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

Writes the given list to the stream. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

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

Reads from the stream into the given list. Returns a reference to the stream.

Refer to Serializing Data Types for additional information.

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