Public Member Functions | Static Public Member Functions | List of all members
QStringList Class Reference

A list of strings. More...

#include <qstringlist.h>

Inheritance diagram for QStringList:
QValueList< QString >

Public Member Functions

 QStringList ()
 
 QStringList (const QStringList &l)
 
 QStringList (const QValueList< QString > &l)
 
 QStringList (const QString &i)
 
 QStringList (const char *i)
 
void sort ()
 
QString join (const QString &sep) const
 
QStringList grep (const QString &str, bool cs=TRUE) const
 
QStringList grep (const QRegExp &expr) const
 
- Public Member Functions inherited from QValueList< QString >
 QValueList ()
 
 QValueList (const QValueList< QString > &l)
 
 ~QValueList ()
 
QValueList< QString > & operator= (const QValueList< QString > &l)
 
QValueList< QStringoperator+ (const QValueList< QString > &l) const
 
QValueList< QString > & operator+= (const QValueList< QString > &l)
 
QValueList< QString > & operator+= (const QString &x)
 
bool operator== (const QValueList< QString > &l) const
 
bool operator!= (const QValueList< QString > &l) const
 
Iterator begin ()
 
ConstIterator begin () const
 
Iterator end ()
 
ConstIterator end () const
 
Iterator fromLast ()
 
ConstIterator fromLast () const
 
bool isEmpty () const
 
Iterator insert (Iterator it, const QString &x)
 
Iterator append (const QString &x)
 
Iterator prepend (const QString &x)
 
Iterator remove (Iterator it)
 
void remove (const QString &x)
 
QStringfirst ()
 
const QStringfirst () const
 
QStringlast ()
 
const QStringlast () const
 
QStringoperator[] (uint i)
 
const QStringoperator[] (uint i) const
 
Iterator at (uint i)
 
ConstIterator at (uint i) const
 
Iterator find (const QString &x)
 
ConstIterator find (const QString &x) const
 
Iterator find (Iterator it, const QString &x)
 
ConstIterator find (ConstIterator it, const QString &x) const
 
int findIndex (const QString &x) const
 
uint contains (const QString &x) const
 
uint count () const
 
void clear ()
 
QValueList< QString > & operator<< (const QString &x)
 

Static Public Member Functions

static QStringList fromStrList (const QStrList &)
 
static QStringList split (const QString &sep, const QString &str, bool allowEmptyEntries=FALSE)
 
static QStringList split (const QCString &sep, const QCString &str, bool allowEmptyEntries=FALSE)
 
static QStringList split (const QChar &sep, const QString &str, bool allowEmptyEntries=FALSE)
 
static QStringList split (const QRegExp &sep, const QString &str, bool allowEmptyEntries=FALSE)
 

Additional Inherited Members

- Public Types inherited from QValueList< QString >
typedef QValueListIterator< QStringIterator
 
typedef QValueListConstIterator< QStringConstIterator
 
typedef QString ValueType
 
- Protected Member Functions inherited from QValueList< QString >
void detach ()
 
- Protected Attributes inherited from QValueList< QString >
QValueListPrivate< QString > * sh
 

Detailed Description

A list of strings.

QStringList is basically a QValueList of QString objects. As opposed to QStrList, that stores pointers to characters, QStringList deals with real QString objects. It is the class of choice whenever you work with unicode strings.

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

Example:

// three different ways of appending values:
list.append( "Torben");
list += "Warwick";
list << "Matthias" << "Arnt" << "Paul";
// sort the list, Arnt's now first
list.sort();
// print it out
for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) {
printf( "%s \n", (*it).latin1() );
}

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

Definition at line 51 of file qstringlist.h.

Constructor & Destructor Documentation

QStringList::QStringList ( )
inline

Creates an empty list.

Definition at line 54 of file qstringlist.h.

54 { }
QStringList::QStringList ( const QStringList l)
inline

Creates a copy of the list. This function is very fast since QStringList is implicit 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, then the modifying list makes a copy first.

Definition at line 55 of file qstringlist.h.

static QStrList * l
Definition: config.cpp:1044
QStringList::QStringList ( const QValueList< QString > &  l)
inline

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

Definition at line 56 of file qstringlist.h.

static QStrList * l
Definition: config.cpp:1044
QStringList::QStringList ( const QString i)
inline

Constructs a string list consisting of the single string i. To make longer lists easily, use:

QString s1,s2,s3;
...
QStringList mylist = QStringList() << s1 << s2 << s3;

Definition at line 57 of file qstringlist.h.

57 { append(i); }
Iterator append(const QString &x)
Definition: qvaluelist.h:372
QStringList::QStringList ( const char *  i)
inline

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

Definition at line 59 of file qstringlist.h.

59 { append(i); }
Iterator append(const QString &x)
Definition: qvaluelist.h:372

Member Function Documentation

QStringList QStringList::fromStrList ( const QStrList ascii)
static

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

Definition at line 298 of file qstringlist.cpp.

299 {
300  QStringList res;
301  const char * s;
302  for ( QStrListIterator it(ascii); (s=it.current()); ++it )
303  res << s;
304  return res;
305 }
A list of strings.
Definition: qstringlist.h:51
static QCString * s
Definition: config.cpp:1042
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, else not.

Definition at line 238 of file qstringlist.cpp.

239 {
240  QStringList res;
241  for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
242  if ( (*it).contains( str, cs ) )
243  res << *it;
244 
245  return res;
246 }
A list of strings.
Definition: qstringlist.h:51
const char * cs
QStringList QStringList::grep ( const QRegExp expr) const

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

Definition at line 253 of file qstringlist.cpp.

254 {
255  QStringList res;
256  for ( QStringList::ConstIterator it = begin(); it != end(); ++it )
257  if ( (*it).contains( expr ) )
258  res << *it;
259 
260  return res;
261 }
A list of strings.
Definition: qstringlist.h:51
QString QStringList::join ( const QString sep) const

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

See also
split()

Definition at line 269 of file qstringlist.cpp.

270 {
271  QString res;
272  bool alredy = FALSE;
273  for ( QStringList::ConstIterator it = begin(); it != end(); ++it ) {
274  if ( alredy )
275  res += sep;
276  alredy = TRUE;
277  res += *it;
278  }
279 
280  return res;
281 }
const bool FALSE
Definition: qglobal.h:370
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
const bool TRUE
Definition: qglobal.h:371
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).

Definition at line 124 of file qstringlist.cpp.

125 {
126  qHeapSort(*this);
127 }
void qHeapSort(InputIterator b, InputIterator e)
Definition: qtl.h:192
QStringList QStringList::split ( const QString sep,
const QString str,
bool  allowEmptyEntries = FALSE 
)
static

Splits the string str using sep as separator. Returns the list of strings. If allowEmptyEntries is TRUE, also empty entries are inserted into the list, else not. So if you have a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e' would be returned if allowEmptyEntries is FALSE, but a list containing 'abc', '', 'd', 'e' and '' would be returned if allowEmptyEntries is TRUE. If str doesn't contain sep, a stringlist with one item, which is the same as str, is returned.

See also
join()

Definition at line 162 of file qstringlist.cpp.

163 {
165 
166  int j = 0;
167  int i = str.find( sep, j );
168 
169  while ( i != -1 ) {
170  if ( str.mid( j, i - j ).length() > 0 )
171  lst << str.mid( j, i - j );
172  else if ( allowEmptyEntries )
173  lst << QString::null;
174  j = i + sep.length();
175  i = str.find( sep, j );
176  }
177 
178  int l = str.length() - 1;
179  if ( str.mid( j, l - j + 1 ).length() > 0 )
180  lst << str.mid( j, l - j + 1 );
181  else if ( allowEmptyEntries )
182  lst << QString::null;
183 
184  return lst;
185 }
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
static QStrList * l
Definition: config.cpp:1044
A list of strings.
Definition: qstringlist.h:51
uint length() const
Definition: qstring.h:679
int find(QChar c, int index=0, bool cs=TRUE) const
Definition: qstring.cpp:12902
static const Null null
Definition: qstring.h:376
QStringList QStringList::split ( const QCString sep,
const QCString str,
bool  allowEmptyEntries = FALSE 
)
static

Definition at line 187 of file qstringlist.cpp.

188 {
189  return split(QString(sep.data()),QString(str.data()),allowEmptyEntries);
190 }
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
const char * data() const
Definition: qcstring.h:207
static QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries=FALSE)
QStringList QStringList::split ( const QChar sep,
const QString str,
bool  allowEmptyEntries = FALSE 
)
static

Splits the string str using sep as separator. Returns the list of strings. If allowEmptyEntries is TRUE, also empty entries are inserted into the list, else not. So if you have a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e' would be returned if allowEmptyEntries is FALSE, but a list containing 'abc', '', 'd', 'e' and '' would be returned if allowEmptyEntries is TRUE. If str doesn't contain sep, a stringlist with one item, which is the same as str, is returned.

See also
join()

Definition at line 143 of file qstringlist.cpp.

144 {
145  return split( QString( sep ), str, allowEmptyEntries );
146 }
The QString class provides an abstraction of Unicode text and the classic C null-terminated char arra...
Definition: qstring.h:350
static QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries=FALSE)
QStringList QStringList::split ( const QRegExp sep,
const QString str,
bool  allowEmptyEntries = FALSE 
)
static

Splits the string str using the regular expression sep as separator. Returns the list of strings. If allowEmptyEntries is TRUE, also empty entries are inserted into the list, else not. So if you have a string 'abc..d.e.', a list which contains 'abc', 'd', and 'e' would be returned if allowEmptyEntries is FALSE, but a list containing 'abc', '', 'd', 'e' and '' would be returned if allowEmptyEntries is TRUE. If str doesn't contain sep, a stringlist with one item, which is the same as str, is returned.

See also
join()

Definition at line 206 of file qstringlist.cpp.

207 {
209 
210  int j = 0;
211  int len = 0;
212  int i = sep.match( str.data(), j, &len );
213 
214  while ( i != -1 ) {
215  if ( str.mid( j, i - j ).length() > 0 )
216  lst << str.mid( j, i - j );
217  else if ( allowEmptyEntries )
218  lst << QString::null;
219  j = i + len;
220  i = sep.match( str.data(), j, &len );
221  }
222 
223  int l = str.length() - 1;
224  if ( str.mid( j, l - j + 1 ).length() > 0 )
225  lst << str.mid( j, l - j + 1 );
226  else if ( allowEmptyEntries )
227  lst << QString::null;
228 
229  return lst;
230 }
QString mid(uint index, uint len=0xffffffff) const
Definition: qstring.cpp:13265
static QStrList * l
Definition: config.cpp:1044
const char * data() const
Definition: qstring.h:542
A list of strings.
Definition: qstringlist.h:51
uint length() const
Definition: qstring.h:679
int match(const QCString &str, int index=0, int *len=0, bool indexIsStart=TRUE) const
Definition: qregexp.cpp:649
static const Null null
Definition: qstring.h:376

The documentation for this class was generated from the following files: