Logo Search packages:      
Sourcecode: qt-x11-free version File versions

QString Class Reference

#include <qstring.h>

Inheritance diagram for QString:

QConstString QConstString

List of all members.


Detailed Description

The QString class provides an abstraction of Unicode text and the classic C ''-terminated char array.

QString uses implicit sharing, which makes it very efficient and easy to use.

In all of the QString methods that take {const char *} parameters, the {const char *} is interpreted as a classic C-style ''-terminated ASCII string. It is legal for the {const char *} parameter to be 0. If the {const char *} is not ''-terminated, the results are undefined. Functions that copy classic C strings into a QString will not copy the terminating '' character. The QChar array of the QString (as returned by unicode()) is generally not terminated by a ''. If you need to pass a QString to a function that requires a C ''-terminated string use latin1().

QString::null A QString that has not been assigned to anything is null, i.e. both the length and data pointer is 0. A QString that references the empty string ("", a single '' char) is empty. Both null and empty QStrings are legal parameters to the methods. Assigning {(const char *) 0} to QString gives a null QString. For convenience, QString::null is a null QString. When sorting, empty strings come first, followed by non-empty strings, followed by null strings. We recommend using {if ( !str.isNull() )} to check for a non-null string rather than {if ( !str )}; see operator!() for an explanation.

Note that if you find that you are mixing usage of QCString, QString, and QByteArray, this causes lots of unnecessary copying and might indicate that the true nature of the data you are dealing with is uncertain. If the data is ''-terminated 8-bit data, use QCString; if it is unterminated (i.e. contains ''s) 8-bit data, use QByteArray; if it is text, use QString.

Lists of strings are handled by the QStringList class. You can split a string into a list of strings using QStringList::split(), and join a list of strings into a single string with an optional separator using QStringList::join(). You can obtain a list of strings from a string list that contain a particular substring or that match a particular regex using QStringList::grep().

Note for C programmers

Due to C++'s type system and the fact that QString is implicitly shared, QStrings can be treated like ints or other simple base types. For example:

    QString boolToString( bool b )
    {
      QString result;
      if ( b )
          result = "True";
      else
          result = "False";
      return result;
    }

The variable, result, is an auto variable allocated on the stack. When return is called, because we're returning by value, The copy constructor is called and a copy of the string is returned. (No actual copying takes place thanks to the implicit sharing, see below.)

Throughout Qt's source code you will encounter QString usages like this:

    QString func( const QString& input )
    {
      QString output = input;
      // process output
      return output;
    }

The 'copying' of input to output is almost as fast as copying a pointer because behind the scenes copying is achieved by incrementing a reference count. QString (like all Qt's implicitly shared classes) operates on a copy-on-write basis, only copying if an instance is actually changed.

If you wish to create a deep copy of a QString without losing any Unicode information then you should use QDeepCopy.

See also:
QChar QCString QByteArray QConstString

Definition at line 397 of file qstring.h.


Public Types

enum  SectionFlags {
  SectionDefault = 0x00, SectionSkipEmpty = 0x01, SectionIncludeLeadingSep = 0x02, SectionIncludeTrailingSep = 0x04,
  SectionCaseInsensitiveSeps = 0x08, SectionDefault = 0x00, SectionSkipEmpty = 0x01, SectionIncludeLeadingSep = 0x02,
  SectionIncludeTrailingSep = 0x04, SectionCaseInsensitiveSeps = 0x08
}
enum  SectionFlags {
  SectionDefault = 0x00, SectionSkipEmpty = 0x01, SectionIncludeLeadingSep = 0x02, SectionIncludeTrailingSep = 0x04,
  SectionCaseInsensitiveSeps = 0x08, SectionDefault = 0x00, SectionSkipEmpty = 0x01, SectionIncludeLeadingSep = 0x02,
  SectionIncludeTrailingSep = 0x04, SectionCaseInsensitiveSeps = 0x08
}

Public Member Functions

QStringappend (const std::string &)
QStringappend (const char *)
QStringappend (const QByteArray &)
QStringappend (const QString &)
QStringappend (QChar)
QStringappend (char)
QStringappend (const std::string &)
QStringappend (const char *)
QStringappend (const QByteArray &)
QStringappend (const QString &)
QStringappend (QChar)
QStringappend (char)
QString arg (const QString &a1, const QString &a2, const QString &a3, const QString &a4) const
QString arg (const QString &a1, const QString &a2, const QString &a3) const
QString arg (const QString &a1, const QString &a2) const
QString arg (const QString &a, int fieldWidth=0) const
QString arg (QChar a, int fieldWidth=0) const
QString arg (char a, int fieldWidth=0) const
QString arg (double a, int fieldWidth=0, char fmt= 'g', int prec=-1) const
QString arg (ushort a, int fieldWidth=0, int base=10) const
QString arg (short a, int fieldWidth=0, int base=10) const
QString arg (uint a, int fieldWidth=0, int base=10) const
QString arg (int a, int fieldWidth=0, int base=10) const
QString arg (Q_ULLONG a, int fieldwidth=0, int base=10) const
QString arg (Q_LLONG a, int fieldwidth=0, int base=10) const
QString arg (ulong a, int fieldWidth=0, int base=10) const
QString arg (long a, int fieldWidth=0, int base=10) const
QString arg (const QString &a1, const QString &a2, const QString &a3, const QString &a4) const
QString arg (const QString &a1, const QString &a2, const QString &a3) const
QString arg (const QString &a1, const QString &a2) const
QString arg (const QString &a, int fieldWidth=0) const
QString arg (QChar a, int fieldWidth=0) const
QString arg (char a, int fieldWidth=0) const
QString arg (double a, int fieldWidth=0, char fmt= 'g', int prec=-1) const
QString arg (ushort a, int fieldWidth=0, int base=10) const
QString arg (short a, int fieldWidth=0, int base=10) const
QString arg (uint a, int fieldWidth=0, int base=10) const
QString arg (int a, int fieldWidth=0, int base=10) const
QString arg (Q_ULLONG a, int fieldwidth=0, int base=10) const
QString arg (Q_LLONG a, int fieldwidth=0, int base=10) const
QString arg (ulong a, int fieldWidth=0, int base=10) const
QString arg (long a, int fieldWidth=0, int base=10) const
const char * ascii () const
const char * ascii () const
QCharRef at (uint i)
QChar at (uint i) const
QCharRef at (uint i)
QChar at (uint i) const
uint capacity () const
uint capacity () const
int compare (const QString &s) const
int compare (const QString &s) const
void compose ()
void compose ()
QChar constref (uint i) const
QChar constref (uint i) const
int contains (const QRegExp &) const
int contains (const QString &str, bool cs=TRUE) const
int contains (const char *str, bool cs=TRUE) const
int contains (char c, bool cs=TRUE) const
int contains (QChar c, bool cs=TRUE) const
int contains (const QRegExp &) const
int contains (const QString &str, bool cs=TRUE) const
int contains (const char *str, bool cs=TRUE) const
int contains (char c, bool cs=TRUE) const
int contains (QChar c, bool cs=TRUE) const
QString copy () const
QString copy () const
const char * data () const
const char * data () const
bool endsWith (const QString &str, bool cs) const
bool endsWith (const QString &str) const
bool endsWith (const QString &str, bool cs) const
bool endsWith (const QString &str) const
QStringfill (QChar c, int len=-1)
QStringfill (QChar c, int len=-1)
int find (const char *str, int index=0) const
int find (const QRegExp &, int index=0) const
int find (const QString &str, int index=0, bool cs=TRUE) const
int find (char c, int index=0, bool cs=TRUE) const
int find (QChar c, int index=0, bool cs=TRUE) const
int find (const char *str, int index=0) const
int find (const QRegExp &, int index=0) const
int find (const QString &str, int index=0, bool cs=TRUE) const
int find (char c, int index=0, bool cs=TRUE) const
int find (QChar c, int index=0, bool cs=TRUE) const
int findRev (const char *str, int index=-1) const
int findRev (const QRegExp &, int index=-1) const
int findRev (const QString &str, int index=-1, bool cs=TRUE) const
int findRev (char c, int index=-1, bool cs=TRUE) const
int findRev (QChar c, int index=-1, bool cs=TRUE) const
int findRev (const char *str, int index=-1) const
int findRev (const QRegExp &, int index=-1) const
int findRev (const QString &str, int index=-1, bool cs=TRUE) const
int findRev (char c, int index=-1, bool cs=TRUE) const
int findRev (QChar c, int index=-1, bool cs=TRUE) const
QStringinsert (uint index, char c)
QStringinsert (uint index, QChar)
QStringinsert (uint index, const QChar *, uint len)
QStringinsert (uint index, const char *)
QStringinsert (uint index, const QByteArray &)
QStringinsert (uint index, const QString &)
QStringinsert (uint index, char c)
QStringinsert (uint index, QChar)
QStringinsert (uint index, const QChar *, uint len)
QStringinsert (uint index, const char *)
QStringinsert (uint index, const QByteArray &)
QStringinsert (uint index, const QString &)
bool isEmpty () const
bool isEmpty () const
bool isNull () const
bool isNull () const
bool isRightToLeft () const
bool isRightToLeft () const
const char * latin1 () const
const char * latin1 () const
QString left (uint len) const
QString left (uint len) const
QString leftJustify (uint width, QChar fill=' ', bool trunc=FALSE) const
QString leftJustify (uint width, QChar fill=' ', bool trunc=FALSE) const
uint length () const
uint length () const
QCString local8Bit () const
QCString local8Bit () const
int localeAwareCompare (const QString &s) const
int localeAwareCompare (const QString &s) const
QString lower () const
QString lower () const
QString mid (uint index, uint len=0xffffffff) const
QString mid (uint index, uint len=0xffffffff) const
 operator const char * () const
 operator const char * () const
 operator std::string () const
 operator std::string () const
bool operator! () const
bool operator! () const
QStringoperator+= (char c)
QStringoperator+= (QChar c)
QStringoperator+= (const std::string &)
QStringoperator+= (const char *str)
QStringoperator+= (const QByteArray &str)
QStringoperator+= (const QString &str)
QStringoperator+= (char c)
QStringoperator+= (QChar c)
QStringoperator+= (const std::string &)
QStringoperator+= (const char *str)
QStringoperator+= (const QByteArray &str)
QStringoperator+= (const QString &str)
QStringoperator= (char c)
QStringoperator= (QChar c)
QStringoperator= (const QCString &)
QStringoperator= (const std::string &)
QStringoperator= (const char *)
QStringoperator= (const QString &)
QStringoperator= (char c)
QStringoperator= (QChar c)
QStringoperator= (const QCString &)
QStringoperator= (const std::string &)
QStringoperator= (const char *)
QStringoperator= (const QString &)
QCharRef operator[] (int i)
QChar operator[] (int i) const
QCharRef operator[] (int i)
QChar operator[] (int i) const
QStringprepend (const std::string &)
QStringprepend (const char *)
QStringprepend (const QByteArray &)
QStringprepend (const QString &)
QStringprepend (QChar)
QStringprepend (char)
QStringprepend (const std::string &)
QStringprepend (const char *)
QStringprepend (const QByteArray &)
QStringprepend (const QString &)
QStringprepend (QChar)
QStringprepend (char)
 QString (const std::string &)
 QString (const char *str)
 QString (const QChar *unicode, uint length)
 QString (const QByteArray &)
 QString (const QString &)
 QString (QChar)
 QString (const std::string &)
 QString (const char *str)
 QString (const QChar *unicode, uint length)
 QString (const QByteArray &)
 QString (const QString &)
 QString (QChar)
 QString ()
QCharref (uint i)
QCharref (uint i)
QStringremove (const QRegExp &)
QStringremove (const char *)
QStringremove (char c)
QStringremove (QChar c)
QStringremove (const QString &, bool cs)
QStringremove (const QString &)
QStringremove (uint index, uint len)
QStringremove (const QRegExp &)
QStringremove (const char *)
QStringremove (char c)
QStringremove (QChar c)
QStringremove (const QString &, bool cs)
QStringremove (const QString &)
QStringremove (uint index, uint len)
QStringreplace (QChar, QChar)
QStringreplace (const QRegExp &, const QString &)
QStringreplace (const QString &, const QString &, bool)
QStringreplace (const QString &, const QString &)
QStringreplace (char c, const QString &after, bool cs)
QStringreplace (char c, const QString &after)
QStringreplace (QChar c, const QString &, bool)
QStringreplace (QChar c, const QString &)
QStringreplace (uint index, uint len, char c)
QStringreplace (uint index, uint len, QChar)
QStringreplace (uint index, uint len, const QChar *, uint clen)
QStringreplace (uint index, uint len, const QString &)
QStringreplace (QChar, QChar)
QStringreplace (const QRegExp &, const QString &)
QStringreplace (const QString &, const QString &, bool)
QStringreplace (const QString &, const QString &)
QStringreplace (char c, const QString &after, bool cs)
QStringreplace (char c, const QString &after)
QStringreplace (QChar c, const QString &, bool)
QStringreplace (QChar c, const QString &)
QStringreplace (uint index, uint len, char c)
QStringreplace (uint index, uint len, QChar)
QStringreplace (uint index, uint len, const QChar *, uint clen)
QStringreplace (uint index, uint len, const QString &)
void reserve (uint minCapacity)
void reserve (uint minCapacity)
QString right (uint len) const
QString right (uint len) const
QString rightJustify (uint width, QChar fill=' ', bool trunc=FALSE) const
QString rightJustify (uint width, QChar fill=' ', bool trunc=FALSE) const
QString section (const QRegExp &reg, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (const QString &in_sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (const char *in_sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (char sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (QChar sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (const QRegExp &reg, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (const QString &in_sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (const char *in_sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (char sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QString section (QChar sep, int start, int end=0xffffffff, int flags=SectionDefault) const
QStringsetAscii (const char *, int len=-1)
QStringsetAscii (const char *, int len=-1)
void setExpand (uint index, QChar c)
void setExpand (uint index, QChar c)
QStringsetLatin1 (const char *, int len=-1)
QStringsetLatin1 (const char *, int len=-1)
void setLength (uint newLength)
void setLength (uint newLength)
QStringsetNum (double, char f='g', int prec=6)
QStringsetNum (float, char f='g', int prec=6)
QStringsetNum (Q_ULLONG, int base=10)
QStringsetNum (Q_LLONG, int base=10)
QStringsetNum (ulong, int base=10)
QStringsetNum (long, int base=10)
QStringsetNum (uint, int base=10)
QStringsetNum (int, int base=10)
QStringsetNum (ushort, int base=10)
QStringsetNum (short, int base=10)
QStringsetNum (double, char f='g', int prec=6)
QStringsetNum (float, char f='g', int prec=6)
QStringsetNum (Q_ULLONG, int base=10)
QStringsetNum (Q_LLONG, int base=10)
QStringsetNum (ulong, int base=10)
QStringsetNum (long, int base=10)
QStringsetNum (uint, int base=10)
QStringsetNum (int, int base=10)
QStringsetNum (ushort, int base=10)
QStringsetNum (short, int base=10)
QStringsetUnicode (const QChar *unicode, uint len)
QStringsetUnicode (const QChar *unicode, uint len)
QStringsetUnicodeCodes (const ushort *unicode_as_ushorts, uint len)
QStringsetUnicodeCodes (const ushort *unicode_as_ushorts, uint len)
bool simpleText () const
bool simpleText () const
QString simplifyWhiteSpace () const
QString simplifyWhiteSpace () const
QStringsprintf (const char *format,...)
QStringsprintf (const char *format,...)
void squeeze ()
void squeeze ()
bool startsWith (const QString &str, bool cs) const
bool startsWith (const QString &str) const
bool startsWith (const QString &str, bool cs) const
bool startsWith (const QString &str) const
QString stripWhiteSpace () const
QString stripWhiteSpace () const
double toDouble (bool *ok=0) const
double toDouble (bool *ok=0) const
float toFloat (bool *ok=0) const
float toFloat (bool *ok=0) const
int toInt (bool *ok=0, int base=10) const
int toInt (bool *ok=0, int base=10) const
long toLong (bool *ok=0, int base=10) const
long toLong (bool *ok=0, int base=10) const
Q_LLONG toLongLong (bool *ok=0, int base=10) const
Q_LLONG toLongLong (bool *ok=0, int base=10) const
short toShort (bool *ok=0, int base=10) const
short toShort (bool *ok=0, int base=10) const
uint toUInt (bool *ok=0, int base=10) const
uint toUInt (bool *ok=0, int base=10) const
ulong toULong (bool *ok=0, int base=10) const
ulong toULong (bool *ok=0, int base=10) const
Q_ULLONG toULongLong (bool *ok=0, int base=10) const
Q_ULLONG toULongLong (bool *ok=0, int base=10) const
ushort toUShort (bool *ok=0, int base=10) const
ushort toUShort (bool *ok=0, int base=10) const
void truncate (uint pos)
void truncate (uint pos)
const unsigned short * ucs2 () const
const unsigned short * ucs2 () const
const QCharunicode () const
const QCharunicode () const
QString upper () const
QString upper () const
QCString utf8 () const
QCString utf8 () const
 ~QString ()

Static Public Member Functions

static int compare (const QString &s1, const QString &s2)
static int compare (const QString &s1, const QString &s2)
static QString fromAscii (const char *, int len=-1)
static QString fromAscii (const char *, int len=-1)
static QString fromLatin1 (const char *, int len=-1)
static QString fromLatin1 (const char *, int len=-1)
static QString fromLocal8Bit (const char *, int len=-1)
static QString fromLocal8Bit (const char *, int len=-1)
static QString fromUcs2 (const unsigned short *ucs2)
static QString fromUcs2 (const unsigned short *ucs2)
static QString fromUtf8 (const char *, int len=-1)
static QString fromUtf8 (const char *, int len=-1)
static int localeAwareCompare (const QString &s1, const QString &s2)
static int localeAwareCompare (const QString &s1, const QString &s2)
static QString number (double, char f='g', int prec=6)
static QString number (uint, int base=10)
static QString number (int, int base=10)
static QString number (Q_ULLONG, int base=10)
static QString number (Q_LLONG, int base=10)
static QString number (ulong, int base=10)
static QString number (long, int base=10)
static QString number (double, char f='g', int prec=6)
static QString number (uint, int base=10)
static QString number (int, int base=10)
static QString number (Q_ULLONG, int base=10)
static QString number (Q_LLONG, int base=10)
static QString number (ulong, int base=10)
static QString number (long, int base=10)

Public Attributes

QT_STATIC_CONST QString null

Private Member Functions

void checkSimpleText () const
void checkSimpleText () const
void deref ()
void deref ()
void detach ()
void detach ()
void grow (uint newLength)
void grow (uint newLength)
QStringinsertHelper (uint index, const char *s, uint len=UINT_MAX)
QStringinsertHelper (uint index, const char *s, uint len=UINT_MAX)
QString multiArg (int numArgs, const QString &a1, const QString &a2, const QString &a3=QString::null, const QString &a4=QString::null) const
QString multiArg (int numArgs, const QString &a1, const QString &a2, const QString &a3=QString::null, const QString &a4=QString::null) const
QStringoperatorPlusEqHelper (const char *s, uint len2=UINT_MAX)
QStringoperatorPlusEqHelper (const char *s, uint len2=UINT_MAX)
 QString (QStringData *dd, bool)
 QString (int size, bool)
 QString (QStringData *dd, bool)
 QString (int size, bool)
void real_detach ()
void real_detach ()
void subat (uint)
void subat (uint)

Static Private Member Functions

static QCharlatin1ToUnicode (const QByteArray &, uint *len)
static QCharlatin1ToUnicode (const char *, uint *len, uint maxlen=(uint)-1)
static QCharlatin1ToUnicode (const QByteArray &, uint *len)
static QCharlatin1ToUnicode (const char *, uint *len, uint maxlen=(uint)-1)
static QStringData * makeSharedNull ()
static QStringData * makeSharedNull ()
static char * unicodeToLatin1 (const QChar *, uint len)
static char * unicodeToLatin1 (const QChar *, uint len)

Private Attributes

QStringData * d

Static Private Attributes

static QStringData * shared_null = 0

Friends

Q_EXPORT QDataStreamoperator>> (QDataStream &, QString &)
Q_EXPORT QDataStreamoperator>> (QDataStream &, QString &)
class QConstString
class QDeepCopy< QString >
class QTextStream

Related Functions

(Note that these are not member functions.)

bool operator!= (const char *s1, const QString &s2)
bool operator!= (const QString &s1, const char *s2)
bool operator!= (const QString &s1, const QString &s2)
const QString operator+ (char c, const QString &s)
const QString operator+ (const QString &s, char c)
const QString operator+ (const char *s1, const QString &s2)
const QString operator+ (const QString &s1, const char *s2)
const QString operator+ (const QString &s1, const QString &s2)
bool operator< (const char *s1, const QString &s2)
bool operator< (const QString &s1, const char *s2)
QDataStreamoperator<< (QDataStream &s, const QString &str)
bool operator<= (const char *s1, const QString &s2)
bool operator<= (const QString &s1, const char *s2)
bool operator== (const char *s1, const QString &s2)
bool operator== (const QString &s1, const char *s2)
bool operator== (const QString &s1, const QString &s2)
bool operator> (const char *s1, const QString &s2)
bool operator> (const QString &s1, const char *s2)
bool operator>= (const char *s1, const QString &s2)
bool operator>= (const QString &s1, const char *s2)

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

Generated by  Doxygen 1.6.0   Back to index