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::QString (const char *str)

Creates a string that is a deep copy of str.

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 (char c, bool cs=TRUE) const

Returns the number of times the character c occurs in the string. The match is case sensitive if and only if cs is TRUE, or case insensitive if cs if FALSE.

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().

bool QString::fill (char c, int len = -1)

Fills the string with len bytes of value c, followed by a '\0'-terminator.

If len is negative, then the current string length will be used.

Returns FALSE is len is nonnegative and there is no memory to resize the string, otherwise TRUE is returned.

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"

QString & QString::insert (uint index, char c)

Insert c into the string at (before) position index. If index is too large, c is inserted at the end of the string.

  QString a = "Yes";
  a.insert( 12528, '!');        // ok, becomes "Yes!"

See also: remove(), replace().

bool QString::isEmpty () const

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

QString QString::left (uint len) const

Returns a substring that contains the len leftmost characters of this string. A deep copy of this string will be returned if len exceeds the length of this string.

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().

uint QString::length () const

Returns the length of the string, excluding the '\0'-terminator. Void strings (null pointers) have zero length.

QString & QString::lower ()

Converts the string to lower case and returns a reference to this string. At present it only handles 7-bit ASCII, or whatever the system tolower() handles (if $LC_CTYPE is set, most unices do the Right Thing).

See also: upper().

To do:

QString QString::mid (uint index, uint len) const

Returns a substring that contains the len characters of this string, starting at position index.

Returns a void string if this string is empty or index is out of range.

bool QString::operator! () const

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

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

Appends str to this string and returns a reference to the string.

QString& QString::operator+= (char c)

Appends c to this string and returns a reference to the string.

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::remove (uint index, uint len)

Removes len characters starting at position index from the string.

If index is too big, nothing happens. If index is valid, but len is too large, the rest of the string is removed.

  QString a = "It's a black rug";
  a.remove( 8, 6 );                     // becomes "It's a bug"

See also: insert(), replace().

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().

bool QString::resize (uint len)

Extends or shrinks the string to len bytes, including the '\0'-terminator. A \0-terminator is set at position len - 1 unless len == 0

QString QString::right (uint len) const

Returns a substring that contains the len rightmost characters of this string. A deep copy of this string will be returned if len exceeds the length of this string.

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().

bool QString::setExpand (uint index, char c)

Sets the characted at position index to c and expands the string is necessary.

Returns FALSE if this index was out of range and the string could not be expanded, otherwise TRUE.

QString & QString::setNum (long n)

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

QString & QString::setNum (ulong n)

Sets the string to the numerical unsigned value of n. Returns a reference to this 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::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::setStr (const char *str)

Makes a deep copy of str without dereferencing the current string, i.e. all strings that share data will be modified. Returns a reference to this 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.

QString & QString::stripWhiteSpace ()

Strips white space from the beginning and the end of the string.

See also: simplifyWhiteSpace().

double QString::toDouble (bool *ok) const

Returns the string converted to a double 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.

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.

long QString::toLong (bool *ok) const

Returns the string converted to a long 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.

ulong QString::toULong (bool *ok) const

Returns the string converted to an unsigned long 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.

QString & QString::upper ()

Converts the string to upper case and returns a reference to the string. At present it only handles 7-bit ASCII, or whatever the system toupper() handles (if $LC_CTYPE is set, most unices do the Right Thing).

See also: lower().

To do:


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:

char * q_strdup (const char *src)

Allocates space for a copy of the string, copies it, and returns a pointer to the copy.

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 16:17, 1995/06/30 by the webmaster at Troll Tech