QString

Section: Misc. Reference Manual Pages (3qt)
Updated: 2 February 2007
Index Return to Main Contents
 

NAME

QString - Abstraction of Unicode text and the classic C '\0'-terminated char array  

SYNOPSIS

All the functions in this class are reentrant when Qt is built with thread support.</p>

#include <qstring.h>

 

Public Members


QString ()

QString ( QChar ch )

QString ( const QString & s )

QString ( const QByteArray & ba )

QString ( const QChar * unicode, uint length )

QString ( const char * str )

QString ( const std::string & str )

~QString ()

QString & operator= ( const QString & s )

QString & operator= ( const char * str )

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

QString & operator= ( const QCString & cstr )

QString & operator= ( QChar c )

QString & operator= ( char c )

bool isNull () const

bool isEmpty () const

uint length () const

void truncate ( uint newLen )

QString & fill ( QChar c, int len = -1 )

QString copy () const (obsolete)

QString arg ( long a, int fieldWidth = 0, int base = 10 ) const

QString arg ( ulong a, int fieldWidth = 0, int base = 10 ) const

QString arg ( Q_LLONG a, int fieldWidth = 0, int base = 10 ) const

QString arg ( Q_ULLONG a, int fieldWidth = 0, int base = 10 ) const

QString arg ( int a, int fieldWidth = 0, int base = 10 ) const

QString arg ( uint a, int fieldWidth = 0, int base = 10 ) const

QString arg ( short a, int fieldWidth = 0, int base = 10 ) const

QString arg ( ushort a, int fieldWidth = 0, int base = 10 ) const

QString arg ( double a, int fieldWidth = 0, char fmt = 'g', int prec = -1 ) const

QString arg ( char a, int fieldWidth = 0 ) const

QString arg ( QChar a, int fieldWidth = 0 ) const

QString arg ( const QString & a, int fieldWidth = 0 ) const

QString arg ( const QString & a1, const QString & a2 ) const

QString arg ( const QString & a1, const QString & a2, const QString & a3 ) const

QString arg ( const QString & a1, const QString & a2, const QString & a3, const QString & a4 ) const

QString & sprintf ( const char * cformat, ... )

int find ( QChar c, int index = 0, bool cs = TRUE ) const

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

int find ( const QString & str, int index = 0, bool cs = TRUE ) const

int find ( const QRegExp & rx, int index = 0 ) const

int find ( const char * str, int index = 0 ) const

int findRev ( QChar c, int index = -1, bool cs = TRUE ) const

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

int findRev ( const QString & str, int index = -1, bool cs = TRUE ) const

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

int findRev ( const char * str, int index = -1 ) const

int contains ( QChar c, bool cs = TRUE ) const

int contains ( char c, bool cs = TRUE ) const

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

int contains ( const QString & str, bool cs = TRUE ) const

int contains ( const QRegExp & rx ) const

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

QString section ( QChar 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 ( const char * sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const

QString section ( const QString & 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 left ( uint len ) const

QString right ( uint len ) const

QString mid ( uint index, uint len = 0xffffffff ) const

QString leftJustify ( uint width, QChar fill = ' ', bool truncate = FALSE ) const

QString rightJustify ( uint width, QChar fill = ' ', bool truncate = FALSE ) const

QString lower () const

QString upper () const

QString stripWhiteSpace () const

QString simplifyWhiteSpace () const

QString & insert ( uint index, const QString & s )

QString & insert ( uint index, const QByteArray & s )

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

QString & insert ( uint index, const QChar * s, uint len )

QString & insert ( uint index, QChar c )

QString & insert ( uint index, char c )

QString & append ( char ch )

QString & append ( QChar ch )

QString & append ( const QString & str )

QString & append ( const QByteArray & str )

QString & append ( const char * str )

QString & append ( const std::string & str )

QString & prepend ( char ch )

QString & prepend ( QChar ch )

QString & prepend ( const QString & s )

QString & prepend ( const QByteArray & s )

QString & prepend ( const char * s )

QString & prepend ( const std::string & s )

QString & remove ( uint index, uint len )

QString & remove ( const QString & str, bool cs = TRUE )

QString & remove ( QChar c )

QString & remove ( char c )

QString & remove ( const char * str )

QString & remove ( const QRegExp & rx )

QString & replace ( uint index, uint len, const QString & s )

QString & replace ( uint index, uint len, const QChar * s, uint slen )

QString & replace ( uint index, uint len, QChar c )

QString & replace ( uint index, uint len, char c )

QString & replace ( QChar c, const QString & after, bool cs = TRUE )

QString & replace ( char c, const QString & after, bool cs = TRUE )

QString & replace ( const QString & before, const QString & after, bool cs = TRUE )

QString & replace ( const QRegExp & rx, const QString & after )

QString & replace ( QChar c1, QChar c2 )

short toShort ( bool * ok = 0, int base = 10 ) const

ushort toUShort ( bool * ok = 0, int base = 10 ) const

int toInt ( bool * ok = 0, int base = 10 ) const

uint toUInt ( bool * ok = 0, int base = 10 ) const

long toLong ( bool * ok = 0, int base = 10 ) const

ulong toULong ( bool * ok = 0, int base = 10 ) const

Q_LLONG toLongLong ( bool * ok = 0, int base = 10 ) const

Q_ULLONG toULongLong ( bool * ok = 0, int base = 10 ) const

float toFloat ( bool * ok = 0 ) const

double toDouble ( bool * ok = 0 ) const

QString & setNum ( short n, int base = 10 )

QString & setNum ( ushort n, int base = 10 )

QString & setNum ( int n, int base = 10 )

QString & setNum ( uint n, int base = 10 )

QString & setNum ( long n, int base = 10 )

QString & setNum ( ulong n, int base = 10 )

QString & setNum ( Q_LLONG n, int base = 10 )

QString & setNum ( Q_ULLONG n, int base = 10 )

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

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

void setExpand ( uint index, QChar c ) (obsolete)

QString & operator+= ( const QString & str )

QString & operator+= ( const QByteArray & str )

QString & operator+= ( const char * str )

QString & operator+= ( const std::string & str )

QString & operator+= ( QChar c )

QString & operator+= ( char c )

QChar at ( uint i ) const

QChar operator[] ( int i ) const

QCharRef at ( uint i )

QCharRef operator[] ( int i )

QChar constref ( uint i ) const

QChar & ref ( uint i )

const QChar * unicode () const

const char * ascii () const

const char * latin1 () const

QCString utf8 () const

QCString local8Bit () const

bool operator! () const

operator const char * () const

operator std::string () const

const unsigned short * ucs2 () const

QString & setUnicode ( const QChar * unicode, uint len )

QString & setUnicodeCodes ( const ushort * unicode_as_ushorts, uint len )

QString & setAscii ( const char * str, int len = -1 )

QString & setLatin1 ( const char * str, int len = -1 )

int compare ( const QString & s ) const

int localeAwareCompare ( const QString & s ) const

void compose ()

const char * data () const (obsolete)

bool startsWith ( const QString & s, bool cs = TRUE ) const

bool endsWith ( const QString & s, bool cs = TRUE ) const

void setLength ( uint newLen )

uint capacity () const

void reserve ( uint minCapacity )

void squeeze ()
 

Static Public Members


QString number ( long n, int base = 10 )

QString number ( ulong n, int base = 10 )

QString number ( Q_LLONG n, int base = 10 )

QString number ( Q_ULLONG n, int base = 10 )

QString number ( int n, int base = 10 )

QString number ( uint n, int base = 10 )

QString number ( double n, char f = 'g', int prec = 6 )

QString fromAscii ( const char * ascii, int len = -1 )

QString fromLatin1 ( const char * chars, int len = -1 )

QString fromUtf8 ( const char * utf8, int len = -1 )

QString fromLocal8Bit ( const char * local8Bit, int len = -1 )

QString fromUcs2 ( const unsigned short * str )

int compare ( const QString & s1, const QString & s2 )

int localeAwareCompare ( const QString & s1, const QString & s2 )
 

RELATED FUNCTION DOCUMENTATION


bool operator== ( const QString & 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 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 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 char * s1, const QString & s2 )

bool operator>= ( const QString & s1, const char * s2 )

bool operator>= ( const char * s1, const QString & s2 )

const QString operator+ ( const QString & s1, const QString & s2 )

const QString operator+ ( const QString & s1, const char * s2 )

const QString operator+ ( const char * s1, const QString & s2 )

const QString operator+ ( const QString & s, char c )

const QString operator+ ( char c, const QString & s )

QDataStream & operator<< ( QDataStream & s, const QString & str )

QDataStream & operator>> ( QDataStream & s, QString & str )
 

DESCRIPTION

The QString class provides an abstraction of Unicode text and the classic C '&#92;0'-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 '&#92;0'-terminated ASCII string. It is legal for the const char * parameter to be 0. If the const char * is not '&#92;0'-terminated, the results are undefined. Functions that copy classic C strings into a QString will not copy the terminating '&#92;0' character. The QChar array of the QString (as returned by unicode()) is generally not terminated by a '&#92;0'. If you need to pass a QString to a function that requires a C '&#92;0'-terminated string use latin1().

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 '&#92;0' 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 '&#92;0'-terminated 8-bit data, use QCString; if it is unterminated (i.e. contains '&#92;0'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, Implicitly and Explicitly Shared Classes, Text Related Classes, and Non-GUI Classes.  

Member Type Documentation

 

QString::SectionFlags

QString::SectionDefault - Empty fields are counted, leading and trailing separators are not included, and the separator is compared case sensitively.
QString::SectionSkipEmpty - Treat empty fields as if they don't exist, i.e. they are not considered as far as start and end are concerned.
QString::SectionIncludeLeadingSep - Include the leading separator (if any) in the result string.
QString::SectionIncludeTrailingSep - Include the trailing separator (if any) in the result string.
QString::SectionCaseInsensitiveSeps - Compare the separator case-insensitively.

Any of the last four values can be OR-ed together to form a flag.

See also section().  

MEMBER FUNCTION DOCUMENTATION

 

QString::QString ()

Constructs a null string, i.e. both the length and data pointer are 0.

See also isNull().  

QString::QString ( QChar ch )

Constructs a string of length one, containing the character ch.  

QString::QString ( const QString & s )

Constructs an implicitly shared copy of s. This is very fast since it only involves incrementing a reference count.  

QString::QString ( const QByteArray & ba )

Constructs a string that is a deep copy of ba interpreted as a classic C string.  

QString::QString ( const QChar * unicode, uint length )

Constructs a string that is a deep copy of the first length characters in the QChar array.

If unicode and length are 0, then a null string is created.

If only unicode is 0, the string is empty but has length characters of space preallocated: QString expands automatically anyway, but this may speed up some cases a little. We recommend using the plain constructor and setLength() for this purpose since it will result in more readable code.

See also isNull() and setLength().  

QString::QString ( const char * str )

Constructs a string that is a deep copy of str, interpreted as a classic C string. The encoding is assumed to be Latin-1, unless you change it using QTextCodec::setCodecForCStrings().

If str is 0, then a null string is created.

This is a cast constructor, but it is perfectly safe: converting a Latin-1 const char * to QString preserves all the information. You can disable this constructor by defining QT_NO_CAST_ASCII when you compile your applications. You can also make QString objects by using setLatin1(), fromLatin1(), fromLocal8Bit(), and fromUtf8(). Or whatever encoding is appropriate for the 8-bit data you have.

See also isNull() and fromAscii().  

QString::QString ( const std::string & str )

Constructs a string that is a deep copy of str.

This is the same as fromAscii(str).  

QString::~QString ()

Destroys the string and frees the string's data if this is the last reference to the string.  

QString & QString::append ( const QString & str )

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


string = "Test";
string.append( "ing" ); // string == "Testing"

Equivalent to operator+=().

Example: dirview/dirview.cpp.  

QString & QString::append ( char ch )

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

Appends character ch to the string and returns a reference to the result.

Equivalent to operator+=().  

QString & QString::append ( QChar ch )

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

Appends character ch to the string and returns a reference to the result.

Equivalent to operator+=().  

QString & QString::append ( const QByteArray & str )

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

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

Equivalent to operator+=().  

QString & QString::append ( const char * str )

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

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

Equivalent to operator+=().  

QString & QString::append ( const std::string & str )

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

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

Equivalent to operator+=().  

QString QString::arg ( const QString & a, int fieldWidth = 0 ) const

This function will return a string that replaces the lowest numbered occurrence of %1, %2, ..., %9 with a.

The fieldWidth value specifies the minimum amount of space that a is padded to. A positive value will produce right-aligned text, whereas a negative value will produce left-aligned text.

The following example shows how we could create a 'status' string when processing a list of files:


QString status = QString( "Processing file %1 of %2: %3" )
.arg( i ) // current file's number
.arg( total ) // number of files to process
.arg( fileName ); // current file's name

It is generally fine to use filenames and numbers as we have done in the example above. But note that using arg() to construct natural language sentences does not usually translate well into other languages because sentence structure and word order often differ between languages.

If there is no place marker (%1, %2, etc.), a warning message (qWarning()) is output and the result is undefined.

Warning: If any placeholder occurs more than once, the result is undefined.  

QString QString::arg ( long a, int fieldWidth = 0, int base = 10 ) const

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

The fieldWidth value specifies the minimum amount of space that a is padded to. A positive value will produce a right-aligned number, whereas a negative value will produce a left-aligned number.

a is expressed in base base, which is 10 by default and must be between 2 and 36.

The '%' can be followed by an 'L', in which case the sequence is replaced with a localized representation of a. The conversion uses the default locale. The default locale is determined from the system's locale settings at application startup. It can be changed using QLocale::setDefault(). The 'L' flag is ignored if base is not 10.


QString str;
str = QString( "Decimal 63 is %1 in hexadecimal" )
.arg( 63, 0, 16 );
// str == "Decimal 63 is 3f in hexadecimal"

QLocale::setDefault(QLocale::English, QLocale::UnitedStates);
str = QString( "%1 %L2 %L3" )
.arg( 12345 )
.arg( 12345 )
.arg( 12345, 0, 16 );
// str == "12345 12,345 3039"
 

QString QString::arg ( ulong a, int fieldWidth = 0, int base = 10 ) const

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

a is expressed in base base, which is 10 by default and must be between 2 and 36. If base is 10, the '%L' syntax can be used to produce localized strings.  

QString QString::arg ( Q_LLONG a, int fieldWidth = 0, int base = 10 ) const

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

a is expressed in base base, which is 10 by default and must be between 2 and 36. If base is 10, the '%L' syntax can be used to produce localized strings.  

QString QString::arg ( Q_ULLONG a, int fieldWidth = 0, int base = 10 ) const

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

a is expressed in base base, which is 10 by default and must be between 2 and 36. If base is 10, the '%L' syntax can be used to produce localized strings.  

QString QString::arg ( int a, int fieldWidth = 0, int base = 10 ) const

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

a is expressed in base base, which is 10 by default and must be between 2 and 36. If base is 10, the '%L' syntax can be used to produce localized strings.  

QString QString::arg ( uint a, int fieldWidth = 0, int base = 10 ) const

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

a is expressed in base base, which is 10 by default and must be between 2 and 36. If base is 10, the '%L' syntax can be used to produce localized strings.  

QString QString::arg ( short a, int fieldWidth = 0, int base = 10 ) const

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

a