QString Class Reference


The QString class is a handle class that provides an abstraction of the C style zero-terminated char array. QString inherits QByteArray, which is defined as QArray<char>. (details) (complete member list)

#include <qstring.h>

Inherits QByteArray.

Public Members

Related Functions

(Note that these are not member functions.)

Detailed Description

The QString class is a handle class that provides an abstraction of the C style zero-terminated char array. QString inherits QByteArray, which is defined as QArray<char>.

Note that for the QString methods that take a const char * parameter the results are undefined if the QString is not zero-terminated. It is legal for the const char * parameter to be 0.

A QString that has not been assigned to anything is void, i.e. both the length and data pointer is 0. A QString that references the empty string ("", a single '\0' char) is empty. Both void and empty QStrings are legal parameters to the methods. Assigning const char * 0 to QString gives a void QString.

Since QString has both an internal length specifier (from QArray) and a zero-terminator, there will be a conflict if somebody manually inserts a '\0' into the string.

  QString s = "networking is fun";
  s[10] = 0;
    // s will be "networking"
    // strlen(s) will be 10
    // s.length() is still 17

See handle classes for information about handle classes.


Member Function Documentation

QString::QString ()

Constructs a void string.

QString::QString (const QString &s)

Constructs a shallow copy s.

QString::QString (int size)

Constructs a string with room for size characters, including the '\0'-terminator.

QString::operator char * () const

Returns the string data.

QString::operator const char * () const

Returns the string data.

int QString::contains (const QRegExp &rx) const

Counts the number of overlapping occurrences of rx in the string.

QString s = "banana and panama";
QRegExp r = QRegExp("a[nm]a", TRUE, FALSE);
s.contains( r );                // 4 matches

See also: find(), findRev().

int QString::contains (const char *str, bool cs=TRUE) const

Returns the number of times str occurs in the string.

The match is case sensitive if cs is TRUE, or case insensitive if cs if FALSE.

This function counts overlapping substrings, for example, "banana" contains two occurrences of "ana".

See also: findRev().

int QString::find (char c, int index=0, bool cs=TRUE) const

Finds the first occurrence of the character c, starting at position index. The search is case sensitive if cs is TRUE and case insensitive if cs is FALSE.

Returns the position of c, or -1 if c could not be found.

int QString::find (const char *str, int index=0, bool cs=TRUE) const

Finds the first occurrence of the string str, starting at position index. The search is case sensitive if cs is TRUE and case insensitive if cs is FALSE.

Returns the position of str, or -1 if str could not be found.

int QString::findRev (const QRegExp &rx, int index=-1) const

Finds the first occurrence of the regular expression rx, starting at position index and searching backwards.

The search will start from the end of the string if index is negative.

Returns the position of the next match (backwards), or -1 if rx was not found.

int QString::findRev (char c, int index=-1, bool cs=TRUE) const

Finds the first occurrence of the character c, starting at position index and searching backwards. The search is case sensitive if cs is TRUE and case insensitive if cs is FALSE.

Returns the position of c, or -1 if c could not be found.

int QString::findRev (const char *str, int index=-1, bool cs=TRUE) const

Finds the first occurrence of the string str, starting at position index and searching backwards. The search is case sensitive if cs is TRUE and case insensitive if cs is FALSE.

Returns the position of str, or -1 if str could not be found.

QString & QString::insert (uint index, const char *s)

Insert s into the string at position index. If index is too large, s is inserted at the end of the string.

  QString a = "I like fish";
  a.insert(a, 2, "don't ");             // becomes: "I don't like fish"

bool QString::isEmpty () const

Returns TRUE is the string is empty (i.e. if length() == 0).

QString QString::leftJustify (uint width, char fill) const

Returns a string of length width (plus '\0') that contains this string and padded by the fill character.

If the length of this string exceeds width, then the result string will be a truncated copy of this string.

  QString s("apple");
  QString t = s.leftJustify(8, '.');      // t == "apple..."

See also: rightJustify().

bool QString::operator! () const

Returns TRUE if it is a null string, otherwise FALSE.

QString & QString::operator= (const QString &s)

Assigns a shallow copy of s and return a reference to this string.

QString & QString::operator= (const char *str)

Assigns a deep copy of str and return a reference to this string.

QString & QString::replace (const QRegExp &rx, const char *str)

Replaces every occurrence of rx in the string with str. Returns a reference to the string.

QString s = "banana";
s.replace( QRegExp("a.*a"), "" );       // becomes "b"

QString & QString::replace (uint index, uint len, const char *s)

Replaces len characters starting at position index from the string with s, and returns a reference to the string.

If index is too big, nothing is deleted and s is inserted at the end of the string. If index is valid, but len is too large, str replaces the rest of the string.

  QString a = "Say yes!";
  a.replace( 4, 3, "NO" );              // becomes "Say NO!"

See also: insert(), remove().

QString QString::rightJustify (uint width, char fill) const

Returns a string of length width (plus '\0') that contains pad characters followed by this string.

If the length of this string exceeds width, then the result string will be a truncated copy of this string.

  QString s("pie");
  QString t = s.rightJustify(8, '.');    // t == ".....pie"

See also: leftJustify().

QString & QString::setNum (ushort n)

Sets the string to the numerical unsigned value of n. Returns a reference to the string.

QString & QString::setNum (double n, char f='g', int prec=6)

Sets the string to the numerical value of n.

Arguments:

Returns a reference to the string.

QString & QString::setNum (float n, char f='g', int prec=6)

Sets the string to the numerical value of n.

Arguments:

Returns a reference to the string.

QString & QString::setNum (int n)

Sets the string to the numerical value of n. Returns a reference to the string.

QString & QString::setNum (uint n)

Sets the string to the numerical unsigned value of n. Returns a reference to the string.

QString & QString::setNum (short n)

Sets the string to the numerical value of n. Returns a reference to the string.

QString & QString::simplifyWhiteSpace ()

Strips white space away from the start and end of the string, and change all internal white space (any sequence of ASCII codes 9, 10, 11, 12, 13 and 32) into a single space.

  QString a = "  lots\t of\nwhite    space ";
  a.simplifyWhiteSpace();               // a: " lots of white space "

See also: stripWhiteSpace().

QString & QString::sprintf (const char *format, ...)

Implemented as a call to the native vsprintf() (see your C-library manual).

Many vsprintf() implementations have some sort of arbitrary and undocumented limit, some crash your program when you exceed it. If your string is shorter than 256 characters, Qt sprintf() calls resize(256) to decrease the chance of crashing.

float QString::toFloat (bool *ok) const

Returns the string converted to a float value.

If ok is non-NULL, *ok will be set to TRUE if there are no conceivable errors, and FALSE if the string is not a number at all, or if it has trailing garbage.

int QString::toInt (bool *ok) const

Returns the string converted to a int value.

If ok is non-NULL, *ok will be set to TRUE if there are no conceivable errors, and FALSE if the string is not a number at all, or if it has trailing garbage.

short QString::toShort (bool *ok) const

Returns the string converted to a short value.

If ok is non-NULL, *ok will be set to TRUE if there are no conceivable errors, and FALSE if the string is not a number at all, or if it has trailing garbage.

uint QString::toUInt (bool *ok) const

Returns the string converted to an unsigned int value.

If ok is non-NULL, *ok will be set to TRUE if there are no conceivable errors, and FALSE if the string is not a number at all, or if it has trailing garbage.

ushort QString::toUShort (bool *ok) const

Returns the string converted to an unsigned short value.

If ok is non-NULL, *ok will be set to TRUE if there are no conceivable errors, and FALSE if the string is not a number at all, or if it has trailing garbage.


Related Functions

void * qmemmove (void *dst, const void *src, uint len)

Some versions of memmove puke on your data if your source and destination blocks overlap.

Not qmemmove.

Arguments:

int qstricmp (const char *str1, const char *str2)

If str1 and str2 are both non-NULL, qstricmp() returns negative, 0 or positive, just like the C library's stricmp(). If either str1 or str2 but not both are NULL, qstricmp() returns a random non-zero value. If both are NULL, qstricmp() returns 0.

See also: qstrnicmp().

int qstrnicmp (const char *str1, const char *str2, uint len)

If str1 and str2 are both non-NULL, qstrnicmp() returns negative, 0 or positive, just like the C library's strnicmp() or strncasecmp. If either str1 or str2 but not both are NULL, qstrnicmp() returns a random non-zero value. If both are NULL, qstrnicmp() returns 0. Also see qstricmp().

See also: qstricmp().


This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.

It was generated from the following files:


Generated at 04:27, 1995/05/20 by the webmaster at Troll Tech