Classes - Annotated - Tree - Functions - Home - Structure

QStringList Class Reference

The QStringList class provides a list of strings. More...

#include <qstringlist.h>

Inherits QValueList<QString>.

List of all member functions.

Public Members

Static Public Members


Detailed Description

The QStringList class provides a list of strings.

It is used to store and manipulate strings that logically belong together. Basically QStringList is a QValueList of QString objects. As opposed to QStrList, which stores pointers to characters, QStringList deals with real QString objects. It is the class of choice whenever you work with Unicode strings. QStringList is part of the Qt Template Library.

Like QString itself, QStringList objects are implicitly shared. Passing them around as value-parameters is both fast and safe.

There are several approaches to add strings to a string list:

        QStringList substitutes;
        substitutes.append( "Times" );
        substitutes +=  "Mincho",
        substitutes << "Arabic Newspaper" << "crox";

To successively access the members of a QStringList use the provided Iterator:

        QStringList substitutions = QFont::substitutes( font.family() );
            QStringList::Iterator i = substitutions.begin();
            while ( i != substitutions.end() ){
                messageText += "<LI>\"" + (* i) + "\"";
                i++;
            }

(Code examples taken from examples/fonts/simple-qfont-demo/viewer.cpp)

Convenience methods such as sort(), split(), join() and grep() make working with QStringLists easy.


Member Function Documentation

QStringList::QStringList ()

Creates an empty list.

QStringList::QStringList ( const QStringList & l )

Creates a copy of the list l. This function is very fast because QStringList is implicitly shared. However, for the programmer this is the same as a deep copy. If this list or the original one or some other list referencing the same shared data is modified, the modifying list first makes a copy.

QStringList::QStringList ( const QValueList<QString> & l )

Constructs a new string list that is a copy of l.

QStringList::QStringList ( const QString & i )

Constructs a string list consisting of the single string i. Longer lists are easily created as follows:

        QStringList comboEntries;
        comboEntries << "one" << "two" << "three" << "four";

(example code taken from table/small-table-demo/main.cpp)

QStringList::QStringList ( const char * i )

Constructs a string list consisting of the single latin-1 string i.

QStringList QStringList::fromStrList ( const QStrList & ascii ) [static]

Converts from a ASCII-QStrList ascii to a QStringList (Unicode).

QStringList QStringList::grep ( const QString & str, bool cs = TRUE ) const

Returns a list of all strings containing the substring str.

If cs is TRUE, the grep is done case-sensitively, otherwise not.

QStringList QStringList::grep ( const QRegExp & expr ) const

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Returns a list of all strings containing a substring that matches the regular expression expr.

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

Joins the stringlist into a single string with each element separated by sep.

See also split().

void QStringList::sort ()

Sorts the list of strings in ascending order.

Sorting is very fast. It uses the Qt Template Library's efficient HeapSort implementation that operates in O(n*log n).

QStringList QStringList::split ( const QRegExp & sep, const QString & str, bool allowEmptyEntries = FALSE ) [static]

Splits the string str into strings where 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.

If sep does not match anywhere in str, split() returns a list consisting of the single string str.

See also join().

Examples: dirview/dirview.cpp and network/httpd/httpd.cpp.

QStringList QStringList::split ( const QString & sep, const QString & str, bool allowEmptyEntries = FALSE ) [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This version of the function uses a QChar as separator, rather than a full regular expression.

If sep is an empty string, the return value is a list of one-character strings: split( QString( "" ), "mfc" ) returns the three-item list "m", "f", "c".

If allowEmptyEntries is TRUE, an empty string is inserted in the list wherever the separator matches twice without intervening text.

QStringList QStringList::split ( const QChar & sep, const QString & str, bool allowEmptyEntries = FALSE ) [static]

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

This version of the function uses a QChar as separator, rather than a full regular expression.

See also join().


Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):


This file is part of the Qt toolkit, copyright © 1995-2001 Trolltech, all rights reserved.


Copyright © 2001 TrolltechTrademarks
Qt version 3.0.0-beta1-beta1