QString(3qt)

๐Ÿ“ฆ version 3.3.8b, in fedora 43, fedora 44

๐Ÿ’– QString ๐Ÿ’–

okay so bestie, you're reading QString(3qt), and honestly? iconic. this is the Qt class, the one everyone texts about. buckle up ๐ŸŽ€


โœจ NAME โœจ

QString โ€” the abstraction of Unicode text, and also the classic C '\0'-terminated char array. two vibes, one class, no cap ๐Ÿ’…


๐ŸŒธ SYNOPSIS ๐ŸŒธ

first things first, the important disclaimer:

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

(yes we're keeping that </p>, it's part of the lore โœจ)

and you get to it like this:

#include <qstring.h>

๐Ÿ‘‘ Public Members

okay so here's the whole cast, in order, exactly as they show up. no skipping, we don't do that here:

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)
heads up: copy() is marked obsolete. she's retired, respect her legacy ๐Ÿซถ

then we get the arg() girlies, and there are SO many of them. she contains multitudes fr fr:

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

now the searching era ๐Ÿ” find her, findRev her, does she contain?:

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

and here's the enum + all the section() slicing, plus the chopping girlies left, right, mid, the justification queens, and the case/whitespace glow-ups:

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

then the editing tools โ€” inserting, appending, prepending, removing, replacing. she's always doing something ๐Ÿ’…:

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 )

now converting her into numbers, and numbers into her. balance โœจ:

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)
setExpand() is also obsolete bestie, she's a legacy queen, don't build new stuff on her ๐Ÿ’€

and then all the operators, accessors, encodings and capacity stuff. the whole finale:

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 ()
data() is obsolete too. three obsolete icons in one class, it's giving vintage ๐ŸŽ€

๐Ÿฆ‹ Static Public Members

the ones you don't even need an instance for. main character energy, zero dependencies โœจ:

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 )

these aren't members but they're still fam, and they get to live here. comparisons first:

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 )

the concatenation cuties (yes, operator+ really returns const QString, that's just how she is):

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 )

and the stream besties, for when you need to send her over a QDataStream ๐ŸŒˆ:

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

okay that's the whole cast list for part one bestie, we are SO not done yet โ€” there's more lore coming, don't go anywhere ๐Ÿ’–

๐Ÿ’– DESCRIPTION ๐Ÿ’–

ok so QString is the class that gives you an abstraction of Unicode text, plus the classic C '\0'-terminated char array. it's giving glow-up for strings, fr fr.

QString uses implicit sharing, which makes it super efficient and genuinely easy to use. no cap. ๐Ÿฆ‹

in every single QString method that takes a const char * parameter, that const char * gets interpreted as a classic C-style '\0'-terminated ASCII string. it is legal for the const char * parameter to be 0. but if the const char * is not '\0'-terminated?? bestie the results are undefined. ๐Ÿ’€ functions that copy classic C strings into a QString will not copy the terminating '\0' character. and the QChar array of the QString (as returned by unicode()) is generally not terminated by a '\0'. so if you need to pass a QString to a function that requires a C '\0'-terminated string, use latin1(). periodt.


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 '\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. iykyk โœจ

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 '\0'-terminated 8-bit data, use QCString; if it is unterminated (i.e. contains '\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(). obsessed. ๐Ÿ’…


๐ŸŒˆ 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. it's giving main character energy for real.

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. slay. โœจ

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. tiny but mighty ๐ŸŽ€

QString::QString ( const QString & s )

constructs an implicitly shared copy of s. this is very fast since it only involves incrementing a reference count. literally the speediest thing ever โšก

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. iykyk ๐Ÿ’…

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. bye bestie ๐Ÿ‘‹๐Ÿ’–

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

It's an overloaded member function, bestie โ€” provided for convenience, and it behaves essentially like the one right above it. ๐Ÿซถ

a gets expressed in base base, which is 10 by default and must be between 2 and 36. And if base is 10, you can hit the '%L' syntax to produce localized strings. ๐ŸŒโœจ


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

Another overloaded member function, provided for convenience, behaving essentially like the above function. no cap ๐Ÿ’…

Same deal: a is expressed in base base, 10 by default, must be between 2 and 36. If base is 10, the '%L' syntax gives you localized strings. ๐ŸŒธ


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

Overloaded member function, provided for convenience, behaves essentially like the above function. โœจ

a is expressed in base base โ€” 10 by default, must be between 2 and 36 โ€” and if base is 10, '%L' syntax = localized strings. ๐ŸŽ€


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

Overloaded member function, provided for convenience, 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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿ’œ

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


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

Overloaded member function, provided for convenience, 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 ( double a, int fieldWidth = 0, char fmt = 'g', int prec = -1 ) const

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿ”ฅ

Argument a is formatted according to the fmt format specified, which is 'g' by default, and can be any of the following:

<center>.nf
</center>
ok so heads up: the format table from the original page did not survive the manpage conversion โ€” that <center>.nf / </center> blob up there is literally all we got. the facts below still apply tho ๐Ÿ’€

With 'e', 'E', and 'f', prec is the number of digits after the decimal point. With 'g' and 'G', prec is the maximum number of significant digits (trailing zeroes are omitted). โœจ

double d = 12.34;
QString ds = QString( "'E' format, precision 3, gives %1" )
                .arg( d, 0, 'E', 3 );
// ds == "'E' format, precision 3, gives 1.234E+01"

The '%L' syntax can be used to produce localized strings. ๐ŸŒ


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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿซถ

a is assumed to be in the Latin-1 character set. ๐ŸŽ€


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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿ’…


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

Overloaded member function, provided for convenience, behaves essentially like the above function. โœจ

This is the same as str.arg(a1).arg(a2), except that the strings are replaced in one pass. And like... that can genuinely make a difference if a1 contains e.g. %1:

QString str( "%1 %2" );
str.arg( "Hello", "world" );        // returns "Hello world"
str.arg( "Hello" ).arg( "world" );  // returns "Hello world"
str.arg( "(%1)", "Hello" );           // returns "(%1) Hello"
str.arg( "(%1)" ).arg( "Hello" );     // returns "(Hello) %2"

See how the last two diverge?? that's the whole one-pass thing doing its thing ๐Ÿ˜ญ๐Ÿ”ฅ


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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿฆ‹

This is the same as calling str.arg(a1).arg(a2).arg(a3), except that the strings are replaced in one pass. ๐Ÿ’ซ


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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐ŸŒธ

This is the same as calling str.arg(a1).arg(a2).arg(a3).arg(a4), except that the strings are replaced in one pass. ๐ŸŒˆ


const char * QString::ascii () const

Returns an 8-bit ASCII representation of the string. ๐ŸŽ€

If a codec has been set using QTextCodec::codecForCStrings(), it is used to convert Unicode to 8-bit char. Otherwise, this function does the same as latin1(). ๐Ÿ‘‘

See also: fromAscii(), latin1(), utf8(), and local8Bit().

Example: network/networkprotocol/nntp.cpp.


QChar QString::at ( uint i ) const

Returns the character at index i, or 0 if i is beyond the length of the string. ๐Ÿ’œ

const QString string( "abcdefgh" );
QChar ch = string.at( 4 );
// ch == 'e'

If the QString is not const (i.e. const QString) or const& (i.e. const QString &), then the non-const overload of at() will be used instead. ๐Ÿ™ˆ


QCharRef QString::at ( uint i )

Overloaded member function, provided for convenience, behaves essentially like the above function. โœจ

This one returns a reference to the character at index i. So you can assign to the resulting reference, or use it right away โ€” but โš ๏ธ it becomes invalid once further modifications are made to the original string.

If i is beyond the length of the string, then the string is expanded with QChar::null. ๐Ÿ’€


uint QString::capacity () const

Returns the number of characters this string can hold in the allocated memory. ๐Ÿ’พโœจ

See also: reserve() and squeeze().


int QString::compare ( const QString & s1, const QString & s2 ) [static]

Lexically compares s1 with s2 and returns an integer less than, equal to, or greater than zero depending on whether s1 is less than, equal to, or greater than s2. ๐Ÿ”ฅ

The comparison is based exclusively on the numeric Unicode values of the characters, and it's very fast โ€” but ngl, it's not what a human would expect. ๐Ÿ˜ญ If you're sorting user-interface strings, consider QString::localeAwareCompare() instead. ๐Ÿซถ

int a = QString::compare( "def", "abc" );   // a > 0
int b = QString::compare( "abc", "def" );   // b < 0
int c = QString::compare( "abc", "abc" );   // c == 0

int QString::compare ( const QString & s ) const

Overloaded member function, provided for convenience, behaves essentially like the above function. โœจ

Lexically compares this string with s and returns an integer less than, equal to, or greater than zero if it is less than, equal to, or greater than s. ๐Ÿ’…


void QString::compose ()

โš ๏ธ Warning: This function is not supported in Qt 3.x. It is provided for experimental and illustrative purposes only. It is mainly of interest to those experimenting with Arabic and other composition-rich texts. ๐Ÿšจ

Applies possible ligatures to a QString. Useful when composition-rich text requires rendering with glyph-poor fonts โ€” but it also makes compositions such as QChar(0x0041) ('A') and QChar(0x0308) (Unicode accent diaresis), giving QChar(0x00c4) (German A Umlaut). ๐ŸŒˆโœจ


QChar QString::constref ( uint i ) const

Returns the QChar at index i by value. ๐ŸŽ€

Equivalent to at(i).

See also: ref().


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

Returns the number of times the character c occurs in the string. ๐Ÿ”ข

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive. ๐Ÿ‘‘

QString string( "Trolltech and Qt" );
int n = string.contains( 't', FALSE );
// n == 3

Examples:


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

Overloaded member function, provided for convenience, behaves essentially like the above function. โœจ


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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿ’ซ

Returns the number of times the string str occurs in the string. ๐Ÿ”ข

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive. ๐ŸŒธ


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

Overloaded member function, provided for convenience, behaves essentially like the above function. ๐Ÿฆ‹

Returns the number of times str occurs in the string. ๐Ÿ”ข

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive. ๐ŸŽ€

This function counts overlapping strings, so in the example below, there are two instances of "ana" in "bananas". ๐Ÿ˜ญ iconic behaviour fr

QString str( "bananas" );
int i = str.contains( "ana" );  // i == 2

See also: findRev().


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

Overloaded member function, provided for convenience, behaves essentially like the above function. โœจ

Returns the number of times the regexp, rx, matches in the string. ๐Ÿ”ข

This function counts overlapping matches, so in the example below, there are four instances of "ana" or "ama". ๐Ÿ’…

QString str = "banana and panama";
QRegExp rxp = QRegExp( "a[nm]a", TRUE, FALSE );
int i = str.contains( rxp );    // i == 4

See also: find() and findRev().


QString QString::copy () const

๐Ÿ’€ This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

In Qt 2.0 and later, all calls to this function are needless. Just remove them. โœ‚๏ธโœจ


const char * QString::data () const

๐Ÿ’€ This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Returns a pointer to a '\0'-terminated classic C string. ๐ŸŽ€

In Qt 1.x, this returned a char* allowing direct manipulation of the string as a sequence of bytes. In Qt 2.x, where QString is a Unicode string, char* conversion constructs a temporary string โ€” and hence direct character operations are meaningless. ๐Ÿ˜ญ


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

Returns TRUE if the string ends with s; otherwise returns FALSE. โœ…โŒ

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive. ๐Ÿ‘‘

QString str( "Bananas" );
str.endsWith( "anas" );         // returns TRUE
str.endsWith( "pple" );         // returns FALSE

See also: startsWith().

Example: chart/main.cpp.


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

Fills the string with len characters of value c, and returns a reference to the string. ๐Ÿ–Œ๏ธโœจ

If len is negative (the default), the current string length is used. ๐Ÿ’œ

QString str;
str.fill( 'g', 5 );      // string == "ggggg"

okay bestie we're back and we are DEEP in the QString lore rn, stay with me ๐Ÿ’–


๐Ÿ’… MEMBER FUNCTION DOCUMENTATION ๐Ÿ’… (still going, obviously)

๐Ÿ” int QString::find ( const QRegExp & rx, int index = 0 ) const

So basically it hunts down the first match of the regular expression rx, and it starts looking from position index. And here's the little quirk that lives rent-free in my head: if index is -1, the search starts at the last character; if it's -2, at the next to last character, and so on and so on. (If you wanna search the other direction, go peep findRev() โœจ)

It hands you back the position of the first match of rx, or -1 if it found absolutely nothing.

QString string( "bananas" );
int i = string.find( QRegExp("an"), 0 );    // i == 1

See also findRev(), replace(), and contains().

Example: network/mail/smtp.cpp.


๐Ÿ” int QString::find ( QChar c, int index = 0, bool cs = TRUE ) const

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

Finds the first occurrence of the character c, starting at position index. If index is -1, the search starts at the last character; if -2, at the next to last character, and so on forever and ever. (See findRev() for searching backwards.)

If cs is TRUE (which is the default, no cap), the search is case sensitive; otherwise it's case insensitive.

Returns the position of c, or -1 if c could not be found. ๐Ÿ’”


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

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

Find character c starting from position index.

If cs is TRUE (the default), the search is case sensitive; otherwise it's case insensitive.


๐Ÿ” int QString::find ( const QString & str, int index = 0, bool cs = TRUE ) const

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

Finds the first occurrence of the string str, starting at position index. If index is -1, the search starts at the last character, if it is -2, at the next to last character, and so on. (See findRev() for searching backwards.)

If cs is TRUE (the default), the search is case sensitive; otherwise it's case insensitive.

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


๐Ÿ” int QString::find ( const char * str, int index = 0 ) const

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

Equivalent to find(QString(str), index). periodt.


๐Ÿ”„ int QString::findRev ( const char * str, int index = -1 ) const

Equivalent to findRev(QString(str), index).


๐Ÿ”„ int QString::findRev ( QChar c, int index = -1, bool cs = TRUE ) const

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

Finds the first occurrence of the character c, starting at position index and searching backwards (the way I reread my own texts ๐Ÿ˜ญ). If the index is -1, the search starts at the last character, if it is -2, at the next to last character, and so on.

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

If cs is TRUE (the default), the search is case sensitive; otherwise it's case insensitive.

QString string( "bananas" );
int i = string.findRev( 'a' );      // i == 5

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

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

Find character c starting from position index and working backwards.

If cs is TRUE (the default), the search is case sensitive; otherwise it's case insensitive.


๐Ÿ”„ int QString::findRev ( const QString & str, int index = -1, bool cs = TRUE ) const

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

Finds the first occurrence of the string str, starting at position index and searching backwards. If the index is -1, the search starts at the last character, if it is -2, at the next to last character, and so on.

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

If cs is TRUE (the default), the search is case sensitive; otherwise it's case insensitive.

QString string("bananas");
int i = string.findRev( "ana" );      // i == 3

๐Ÿ”„ int QString::findRev ( const QRegExp & rx, int index = -1 ) const

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

Finds the first match of the regexp rx, starting at position index and searching backwards. If the index is -1, the search starts at the last character, if it is -2, at the next to last character, and so on. (See findRev() for searching backwards. yes it says that. iconic of it honestly.)

Returns the position of the match, or -1 if no match was found.

QString string( "bananas" );
int i = string.findRev( QRegExp("an") );      // i == 3

See also find().


โœจ QString QString::fromAscii ( const char * ascii, int len = -1 ) [static]

Returns the Unicode string decoded from the first len bytes of ascii, ignoring the rest of ascii. If len is -1 then the length of ascii is used. And if len is bigger than the length of ascii, then it will just use the length of ascii (it's not gonna invent characters, bestie, it's not a magician ๐Ÿงš).

If a codec has been set using QTextCodec::codecForCStrings(), it is used to convert the string from 8-bit characters to Unicode. Otherwise, this function does the same as fromLatin1().

This is the same as the QString(const char*) constructor, BUT โ€” and this is the plot twist โ€” you can make that constructor invisible if you compile with the define QT_NO_CAST_ASCII, in which case you can explicitly create a QString from 8-bit ASCII text using this function. slay for control freaks ๐Ÿ’…

QString str = QString::fromAscii( "123456789", 5 );
// str == "12345"

โœจ QString QString::fromLatin1 ( const char * chars, int len = -1 ) [static]

Returns the Unicode string decoded from the first len bytes of chars, ignoring the rest of chars. If len is -1 then the length of chars is used. If len is bigger than the length of chars then it will use the length of chars.

See also fromAscii().

Examples:


๐ŸŒ QString QString::fromLocal8Bit ( const char * local8Bit, int len = -1 ) [static]

Returns the Unicode string decoded from the first len bytes of local8Bit, ignoring the rest of local8Bit. If len is -1 then the length of local8Bit is used. If len is bigger than the length of local8Bit then it will use the length of local8Bit.

QString str = QString::fromLocal8Bit( "123456789", 5 );
// str == "12345"

local8Bit is assumed to be encoded in a locale-specific format.

See QTextCodec for more diverse coding/decoding of Unicode strings. ๐ŸŒˆ


๐ŸŒŸ QString QString::fromUcs2 ( const unsigned short * str ) [static]

Constructs a string that is a deep copy of str, interpreted as a UCS2 encoded, zero terminated, Unicode string. (deep copy = it's not borrowing, it owns it, main character energy ๐Ÿ‘‘)

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

See also isNull().


๐ŸŒŸ QString QString::fromUtf8 ( const char * utf8, int len = -1 ) [static]

Returns the Unicode string decoded from the first len bytes of utf8, ignoring the rest of utf8. If len is -1 then the length of utf8 is used. If len is bigger than the length of utf8 then it will use the length of utf8.

QString str = QString::fromUtf8( "123456789", 5 );
// str == "12345"

See QTextCodec for more diverse coding/decoding of Unicode strings.

Example: fonts/simple-qfont-demo/viewer.cpp.


๐Ÿชก QString & QString::insert ( uint index, const QString & s )

Inserts s into the string at position index.

If index is beyond the end of the string, the string is extended with spaces to length index and s is then appended, and it returns a reference to the string.

QString string( "I like fish" );
str = string.insert( 2, "don't " );
// str == "I don't like fish"

See also remove() and replace().

Examples:


๐Ÿชก QString & QString::insert ( uint index, const QByteArray & s )

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

Inserts s into the string at position index and returns a reference to the string.


๐Ÿชก QString & QString::insert ( uint index, const char * s )

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

Inserts s into the string at position index and returns a reference to the string.


๐Ÿชก QString & QString::insert ( uint index, const QChar * s, uint len )

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

Inserts the first len characters in s into the string at position index and returns a reference to the string.


๐Ÿชก QString & QString::insert ( uint index, QChar c )

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

Insert c into the string at position index and returns a reference to the string.

If index is beyond the end of the string, the string is extended with spaces (ASCII 32) to length index and c is then appended.


๐Ÿชก QString & QString::insert ( uint index, char c )

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

Insert character c at position index.


๐Ÿซ™ bool QString::isEmpty () const

Returns TRUE if the string is empty, i.e. if length() == 0; otherwise returns FALSE. Null strings are also empty, so like, empty is the bigger vibe category here ๐Ÿ’€

QString a( "" );
a.isEmpty();        // TRUE
a.isNull();         // FALSE
QString b;
b.isEmpty();        // TRUE
b.isNull();         // TRUE

See also isNull() and length().

Examples:


๐Ÿ•ณ๏ธ bool QString::isNull () const

Returns TRUE if the string is null; otherwise returns FALSE. A null string is always empty.

QString a;          // a.unicode() == 0, a.length() == 0
a.isNull();         // TRUE, because a.unicode() == 0
a.isEmpty();        // TRUE, because a.length() == 0

See also isEmpty() and length().

Examples:


๐Ÿฅ€ const char * QString::latin1 () const

Returns a Latin-1 representation of the string. And okay so this is important: the returned value is undefined if the string contains non-Latin-1 characters. like, undefined undefined. don't test it. ๐Ÿ™ˆ If you want to convert strings into formats other than Unicode, see the QTextCodec classes.

This function is mainly useful for boot-strapping legacy code to use Unicode. (it's giving "helping your dad's old codebase move on" ๐Ÿซถ)

The result remains valid so long as one unmodified copy of the source string exists.

See also fromLatin1(), ascii(), utf8(), and local8Bit().

Examples:


๐Ÿ QString QString::left ( uint len ) const

Returns a substring that contains the len leftmost characters of the string. (leftmost = the beginning, bestie, the front of the line ๐Ÿ‘‘)

The whole string is returned if len exceeds the length of the string. no error, no drama, just the whole thing.

QString s = "Pineapple";
QString t = s.left( 4 );    // t == "Pine"

See also right(), mid(), and isEmpty().

Example: themes/themes.cpp.


still not done, there's more coming, don't you dare close this tab ๐Ÿ’–

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

ok so this one gives you back a string that's exactly width long, padded out with the fill character ๐Ÿ’…

  • if truncate is FALSE and your string is longer than width? bestie you just get a copy of the string. no shrinking, no shame.
  • if truncate is TRUE and your string is longer than width, then every character in the copy past position width gets yeeted, and the copy comes back to you.
QString s( "apple" );
QString t = s.leftJustify( 8, '.' );        // t == "apple..."

see also rightJustify(). ๐Ÿซถ


uint QString::length () const

gives you the length of the string. that's it, that's the whole thing ๐Ÿ’–

null strings AND empty strings both have zero length, so likeโ€ฆ don't come for me, zero is zero.

see also isNull() and isEmpty().

examples:


QCString QString::local8Bit () const

returns the string encoded in a locale-specific format โœจ and the flavor of locale-specific depends on where you're at:

  • on X11, it's the QTextCodec::codecForLocale().
  • on Windows, it's a system-defined encoding.
  • on Mac OS X, it always uses UTF-8 as the encoding.

for more diverse coding/decoding of Unicode strings, go see QTextCodec.

see also fromLocal8Bit(), ascii(), latin1(), and utf8().


int QString::localeAwareCompare ( const QString & s1, const QString & s2 ) [static]

compares s1 with s2 and hands you back an integer that's less than, equal to, or greater than zero depending on whether s1 is less than, equal to, or greater than s2. classic.

the comparison is done in a locale-and-platform-dependent way. use this one when you're presenting sorted lists of strings to the user, because your users deserve it ๐Ÿ’…

see also QString::compare() and QTextCodec::locale().


int QString::localeAwareCompare ( const QString & s ) const

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

compares this string with s.


QString QString::lower () const

returns a lowercase copy of the string. lowercase era ๐Ÿฆ‹

QString string( "TROlltECH" );
str = string.lower();   // str == "trolltech"

see also upper().

example: scribble/scribble.cpp.


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

returns a string containing the len characters of this string, starting at position index. it's giving scissors โœ‚๏ธ

  • returns a null string if the string is empty or if index is out of range.
  • returns the whole string from index if index + len exceeds the length of the string.
QString s( "Five pineapples" );
QString t = s.mid( 5, 4 );                  // t == "pine"

see also left() and right().

examples:


QString QString::number ( long n, int base = 10 ) [static]

a convenience function that returns a string equivalent of the number n to base base, which is 10 by default and must be between 2 and 36. the returned string is in "C" locale. slay.

long a = 63;
QString str = QString::number( a, 16 );             // str == "3f"
QString str = QString::number( a, 16 ).upper();     // str == "3F"

see also setNum().

examples:


QString QString::number ( ulong n, int base = 10 ) [static]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

see also setNum().


QString QString::number ( Q_LLONG n, int base = 10 ) [static]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

see also setNum().


QString QString::number ( Q_ULLONG n, int base = 10 ) [static]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

see also setNum().


QString QString::number ( int n, int base = 10 ) [static]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

see also setNum().


QString QString::number ( uint n, int base = 10 ) [static]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

a convenience factory function that returns a string representation of the number n to the base base, which is 10 by default and must be between 2 and 36.

see also setNum().


QString QString::number ( double n, char f = 'g', int prec = 6 ) [static]

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

argument n is formatted according to the f format specified, which is g by default, and can be any of the following:

<center>.nf
</center>

(yeah bestie the table of formats did NOT survive the trip, that's literally what's in the page ๐Ÿ˜ญ no cap)

with 'e', 'E', and 'f', prec is the number of digits after the decimal point. with 'g' and 'G', prec is the maximum number of significant digits (trailing zeroes are omitted).

double d = 12.34;
QString ds = QString( "'E' format, precision 3, gives %1" )
                .arg( d, 0, 'E', 3 );
// ds == "1.234E+001"

see also setNum().


QString::operator const char * () const

returns ascii(). be sure to read the warnings documented in the ascii() function, i'm not playing around ๐Ÿ™ˆ

ngl, for new code where you want to stay strictly Unicode-clean, you can define the macro QT_NO_ASCII_CAST when compiling your code to hide this function so automatic casts don't happen. bonus: you also catch the programming error described in operator!(). two birds, one vibe check ๐ŸŽ€


QString::operator std::string () const

returns ascii() as a std::string.

Warning: this function may cause an application to crash if a static C run-time is in use. ๐Ÿ’€ this can happen in Microsoft Visual C++ if Qt is configured as single-threaded. a safe alternative is to call ascii() directly and construct a std::string manually.

bool QString::operator! () const

returns TRUE if this is a null string; otherwise returns FALSE.

QString name = getName();
if ( !name )
    name = "Rodney";

note that if you say

QString name = getName();
if ( name )
    doSomethingWith(name);

it will call "operator const char*()", which is inefficent (yes, that's how the page spells it, we're keeping it ๐Ÿ’œ); you may wish to define the macro QT_NO_ASCII_CAST when writing code which you wish to remain Unicode-clean.

when you want the above semantics, use:

QString name = getName();
if ( !name.isNull() )
    doSomethingWith(name);

see also isEmpty().


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

appends str to the string and returns a reference to the string. adding to the collection, iconic behavior โœจ


QString & QString::operator+= ( 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 string.


QString & QString::operator+= ( 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 string.


QString & QString::operator+= ( 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 string.


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

appends c to the string and returns a reference to the string.


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

appends c to the string and returns a reference to the string.


QString & QString::operator= ( QChar c )

sets the string to contain just the single character c. one character, main character energy ๐Ÿ‘‘


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

assigns a shallow copy of s to this string and returns a reference to this string. this is very fast because the string isn't actually copied. copy-on-write girlies stay winning ๐Ÿ’…


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

assigns a deep copy of str, interpreted as a classic C string, to this string and returns a reference to this string.

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

see also isNull().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

makes a deep copy of s and returns a reference to the deep copy.


QString & QString::operator= ( const QCString & cstr )

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

assigns a deep copy of cstr, interpreted as a classic C string, to this string. returns a reference to this string.


QString & QString::operator= ( char c )

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

sets the string to contain just the single character c.


QChar QString::operator[] ( int i ) const

returns the character at index i, or QChar::null if i is beyond the length of the string. no crashing, just nulls, we love a safe queen ๐ŸŒธ

if the QString is not const (i.e., const QString) or const& (i.e., const QString&), then the non-const overload of operator[] will be used instead.


QCharRef QString::operator[] ( int i )

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

the function returns a reference to the character at index i. the resulting reference can then be assigned to, or used immediately, but it will become invalid once further modifications are made to the original string. โš ๏ธ so use it now, not later.

if i is beyond the length of the string, then the string is expanded with QChar::nulls, so that the QCharRef references a valid (null) character in the string.

the QCharRef internal class can be used much like a constant QChar, but if you assign to it, you change the original string (which will detach itself because of QString's copy-on-write semantics). you will get compilation errors if you try to use the result as anything but a QChar. compiler said no โŒ


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

inserts s at the beginning of the string and returns a reference to the string.

equivalent to insert(0, s).

QString string = "42";
string.prepend( "The answer is " );
// string == "The answer is 42"

see also insert().


QString & QString::prepend ( char ch )

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

inserts ch at the beginning of the string and returns a reference to the string.

equivalent to insert(0, ch).

see also insert().

  • QString & QString::prepend ( QChar ch ) โ€” bestie this is an overloaded member function, it's just here for convenience and behaves essentially like the above function.

Inserts ch at the beginning of the string and hands you back a reference to the string. ๐Ÿ’ซ

Equivalent to insert(0, ch).

See also insert().

  • QString & QString::prepend ( const QByteArray & s ) โ€” another overloaded member function, provided for convenience, behaving essentially like the above function.

Inserts s at the beginning of the string and returns a reference to the string.

Equivalent to insert(0, s).

See also insert().

  • QString & QString::prepend ( const char * s ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Inserts s at the beginning of the string and returns a reference to the string.

Equivalent to insert(0, s).

See also insert().

  • QString & QString::prepend ( const std::string & s ) โ€” yep, overloaded member function, provided for convenience, behaves essentially like the above function.

Inserts s at the beginning of the string and returns a reference to the string.

Equivalent to insert(0, s).

See also insert().


QString::ref ( uint i ) ๐Ÿ’…

Returns the QChar at index i by reference, expanding the string with QChar::null if necessary. The resulting reference can be assigned to, or otherwise used immediately, but becomes invalid once furher modifications are made to the string. ๐Ÿ™ˆ (yes bestie, "furher" โ€” that's literally what the docs say, we keep it fr fr)

QString string("ABCDEF");
QChar ch = string.ref( 3 );         // ch == 'D'

See also constref().


QString::remove ( uint index, uint len ) ๐Ÿงš

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

If index is beyond the length of the string, nothing happens. If index is within the string, but index + len is beyond the end of the string, the string is truncated at position index. โœ‚๏ธ

QString string( "Montreal" );
string.remove( 1, 4 );      // string == "Meal"

See also insert() and replace().


  • QString & QString::remove ( const QString & str, bool cs = TRUE ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Removes every occurrence of str in the string. Returns a reference to the string.

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive.

This is the same as replace(str, "", cs).

  • QString & QString::remove ( QChar c ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Removes every occurrence of the character c in the string. Returns a reference to the string.

This is the same as replace(c, "").

  • QString & QString::remove ( char c ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Removes every occurrence of the character c in the string. Returns a reference to the string.

This is the same as replace(c, "").

  • QString & QString::remove ( const char * str ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Removes every occurrence of str in the string. Returns a reference to the string.

  • QString & QString::remove ( const QRegExp & rx ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Removes every occurrence of the regular expression rx in the string. Returns a reference to the string.

This is the same as replace(rx, "").


QString::replace ( uint index, uint len, const QString & s ) โœจ

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

If index is beyond the length of the string, nothing is deleted and s is appended at the end of the string. If index is valid, but index + len is beyond the end of the string, the string is truncated at position index, then s is appended at the end.

QString string( "Say yes!" );
string = string.replace( 4, 3, "NO" );
// string == "Say NO!"
โš ๏ธ Warning: Qt 3.3.3 and earlier had different semantics for the case index >= length(), which contradicted the documentation. To avoid portability problems between Qt 3 versions and with Qt 4, we recommend that you never call the function with index >= length(). bestie do NOT do it, i'm begging ๐Ÿ˜ญ

See also insert() and remove().

Examples:

  • QString & QString::replace ( uint index, uint len, const QChar * s, uint slen ) โ€” this is an overloaded member function, provided for convenience. It behaves essentially like the above function.

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

See also insert() and remove().

  • QString & QString::replace ( uint index, uint len, QChar c ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

This is the same as replace(index, len, QString(c)).

  • QString & QString::replace ( uint index, uint len, char c ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

This is the same as replace(index, len, QChar(c)).

  • QString & QString::replace ( QChar c, const QString & after, bool cs = TRUE ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Replaces every occurrence of the character c in the string with after. Returns a reference to the string.

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive.

Example:

QString s = "a,b,c";
s.replace( QChar(','), " or " );
// s == "a or b or c"
  • QString & QString::replace ( char c, const QString & after, bool cs = TRUE ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Replaces every occurrence of the character c in the string with after. Returns a reference to the string.

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive.

  • QString & QString::replace ( const QString & before, const QString & after, bool cs = TRUE ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Replaces every occurrence of the string before in the string with the string after. Returns a reference to the string.

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive.

Example:

QString s = "Greek is Greek";
s.replace( "Greek", "English" );
// s == "English is English"
  • QString & QString::replace ( const QRegExp & rx, const QString & after ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Replaces every occurrence of the regexp rx in the string with after. Returns a reference to the string. For example:

QString s = "banana";
s.replace( QRegExp("an"), "" );
// s == "ba"

For regexps containing capturing parentheses, occurrences of \1, \2, ..., in after are replaced with rx.cap(1), cap(2), ... ๐Ÿ”ฅ

QString t = "A <i>bon mot</i>.";
t.replace( QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}" );
// t == "A \\emph{bon mot}."

See also find(), findRev(), and QRegExp::cap().

  • QString & QString::replace ( QChar c1, QChar c2 ) โ€” overloaded member function, provided for convenience, behaves essentially like the above function.

Replaces every occurrence of c1 with the char c2. Returns a reference to the string.


QString::reserve ( uint minCapacity ) ๐ŸŽ€

Ensures that at least minCapacity characters are allocated to the string.

This function is useful for code that needs to build up a long string and wants to avoid repeated reallocation. In this example, we want to add to the string until some condition is true, and we're fairly sure that size is big enough:

QString result;
int len = 0;
result.reserve(maxLen);
while (...) {
    result[len++] = ...         // fill part of the space
}
result.squeeze();

If maxLen is an underestimate, the worst that will happen is that the loop will slow down. ๐ŸŒ (not the end of the world bestie)

If it is not possible to allocate enough memory, the string remains unchanged.

See also capacity(), squeeze(), and setLength().


QString::right ( uint len ) const ๐Ÿฆ‹

Returns a string that contains the len rightmost characters of the string.

If len is greater than the length of the string then the whole string is returned.

QString string( "Pineapple" );
QString t = string.right( 5 );   // t == "apple"

See also left(), mid(), and isEmpty().

Example: fileiconview/qfileiconview.cpp.


QString::rightJustify ( uint width, QChar fill = ' ', bool truncate = FALSE ) const ๐ŸŒธ

Returns a string of length width that contains the fill character followed by the string.

If truncate is FALSE and the length of the string is more than width, then the returned string is a copy of the string.

If truncate is TRUE and the length of the string is more than width, then the resulting string is truncated at position width.

QString string( "apple" );
QString t = string.rightJustify( 8, '.' );  // t == "...apple"

See also leftJustify().

โœ‚๏ธ QString QString::section ( QChar sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const

okay bestie this one hands you back a section of the string, and it's lowkey so useful ๐Ÿ’…

This string is treated as a sequence of fields separated by the character, sep. The returned string is the fields from position start to position end inclusive. If end is not specified, all fields from position start to the end of the string are included. Fields are numbered 0, 1, 2, etc., counting from the left, and -1, -2, etc., counting from right to left.

The flags argument can affect some aspects of the function's behaviour, e.g. whether to be case sensitive, whether to skip empty fields and how to deal with leading and trailing separators; see SectionFlags.

    QString csv( "forename,middlename,surname,phone" );
    QString s = csv.section( ',', 2, 2 );   // s == "surname"
    QString path( "/usr/local/bin/myapp" ); // First field is empty
    QString s = path.section( '/', 3, 4 );  // s == "bin/myapp"
    QString s = path.section( '/', 3, 3, SectionSkipEmpty ); // s == "myapp"

If start or end is negative, we count fields from the right of the string, the right-most field being -1, the one from right-most field being -2, and so on.

    QString csv( "forename,middlename,surname,phone" );
    QString s = csv.section( ',', -3, -2 );  // s == "middlename,surname"
    QString path( "/usr/local/bin/myapp" ); // First field is empty
    QString s = path.section( '/', -1 ); // s == "myapp"

See also QStringList::split().

Examples:


๐Ÿ“ QString QString::section ( char sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const

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


๐Ÿ“ QString QString::section ( const char * sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const

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


๐ŸŒˆ QString QString::section ( const QString & sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const

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

This function returns a section of the string.

This string is treated as a sequence of fields separated by the string, sep. The returned string consists of the fields from position start to position end inclusive. If end is not specified, all fields from position start to the end of the string are included. Fields are numbered 0, 1, 2, etc., counting from the left, and -1, -2, etc., counting from right to left.

The flags argument can be used to affect some aspects of the function's behaviour, e.g. whether to be case sensitive, whether to skip empty fields and how to deal with leading and trailing separators; see SectionFlags.

    QString data( "forename**middlename**surname**phone" );
    QString s = data.section( "**", 2, 2 ); // s == "surname"

If start or end is negative, we count fields from the right of the string, the right-most field being -1, the one from right-most field being -2, and so on.

    QString data( "forename**middlename**surname**phone" );
    QString s = data.section( "**", -3, -2 ); // s == "middlename**surname"

See also QStringList::split().


๐Ÿงš QString QString::section ( const QRegExp & reg, int start, int end = 0xffffffff, int flags = SectionDefault ) const

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

This function returns a section of the string.

This string is treated as a sequence of fields separated by the regular expression, reg. The returned string consists of the fields from position start to position end inclusive. If end is not specified, all fields from position start to the end of the string are included. Fields are numbered 0, 1, 2, etc., counting from the left, and -1, -2, etc., counting from right to left.

The flags argument can be used to affect some aspects of the function's behaviour, e.g. whether to be case sensitive, whether to skip empty fields and how to deal with leading and trailing separators; see SectionFlags.

    QString line( "forename\tmiddlename  surname \t \t phone" );
    QRegExp sep( "\s+" );
    QString s = line.section( sep, 2, 2 ); // s == "surname"

If start or end is negative, we count fields from the right of the string, the right-most field being -1, the one from right-most field being -2, and so on.

    QString line( "forename\tmiddlename  surname \t \t phone" );
    QRegExp sep( "\\s+" );
    QString s = line.section( sep, -3, -2 ); // s == "middlename  surname"
โš ๏ธ Warning: Using this QRegExp version is much more expensive than the overloaded string and character versions. like, she's gorgeous but she's pricey bestie ๐Ÿ˜ญ

See also QStringList::split() and simplifyWhiteSpace().


๐Ÿ’œ QString & QString::setAscii ( const char * str, int len = -1 )

Sets this string to str, interpreted as a classic 8-bit ASCII C string. If len is -1 (the default), then it is set to strlen(str).

If str is 0 a null string is created. If str is "", an empty string is created.

See also isNull() and isEmpty().


๐Ÿ’€ void QString::setExpand ( uint index, QChar c )

This function is obsolete. It is provided to keep old source working. We strongly advise against using it in new code.

Sets the character at position index to c and expands the string if necessary, filling with spaces.

This method is redundant in Qt 3.x, because operator[] will expand the string as necessary.


๐ŸŒธ QString & QString::setLatin1 ( const char * str, int len = -1 )

Sets this string to str, interpreted as a classic Latin-1 C string. If len is -1 (the default), then it is set to strlen(str).

If str is 0 a null string is created. If str is "", an empty string is created.

See also isNull() and isEmpty().


๐ŸŽ€ void QString::setLength ( uint newLen )

Ensures that at least newLen characters are allocated to the string, and sets the length of the string to newLen. Any new space allocated contains arbitrary data. (so like, don't trust it, she's chaotic ๐Ÿ™ˆ)

See also reserve() and truncate().


โญ QString & QString::setNum ( Q_LLONG n, int base = 10 )

Sets the string to the printed value of n in base base and returns a reference to the string. The returned string is in "C" locale.

The base is 10 by default and must be between 2 and 36.

        QString string;
        string = string.setNum( 1234 );     // string == "1234"

โญ QString & QString::setNum ( short n, int base = 10 )

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

Sets the string to the printed value of n in base base and returns a reference to the string.

The base is 10 by default and must be between 2 and 36.


โญ QString & QString::setNum ( ushort n, int base = 10 )

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

Sets the string to the printed value of n in base base and returns a reference to the string.

The base is 10 by default and must be between 2 and 36.


โญ QString & QString::setNum ( int n, int base = 10 )

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

Sets the string to the printed value of n in base base and returns a reference to the string.

The base is 10 by default and must be between 2 and 36.


โญ QString & QString::setNum ( uint n, int base = 10 )

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

Sets the string to the printed value of n in base base and returns a reference to the string.

The base is 10 by default and must be between 2 and 36.


โญ QString & QString::setNum ( long n, int base = 10 )

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


โญ QString & QString::setNum ( ulong n, int base = 10 )

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


โญ QString & QString::setNum ( Q_ULLONG n, int base = 10 )

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

Sets the string to the printed value of n in base base and returns a reference to the string.

The base is 10 by default and must be between 2 and 36.


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

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

Sets the string to the printed value of n, formatted in format f with precision prec, and returns a reference to the string.

The format f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See arg() for an explanation of the formats.


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

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

Sets the string to the printed value of n, formatted in format f with precision prec, and returns a reference to the string.

The format f can be 'f', 'F', 'e', 'E', 'g' or 'G'. See arg() for an explanation of the formats.


๐Ÿ‘‘ QString & QString::setUnicode ( const QChar * unicode, uint len )

Resizes the string to len characters and copies unicode into the string. If unicode is 0, nothing is copied, but the string is still resized to len. If len is zero, then the string becomes a null string.

See also setLatin1() and isNull().


๐Ÿ‘‘ QString & QString::setUnicodeCodes ( const ushort * unicode_as_ushorts, uint len )

Resizes the string to len characters and copies unicode_as_ushorts into the string (on some X11 client platforms this will involve a byte-swapping pass).

If unicode_as_ushorts is 0, nothing is copied, but the string is still resized to len. If len is zero, the string becomes a null string.

See also setLatin1() and isNull().


๐ŸŒˆ QString QString::simplifyWhiteSpace () const

Returns a string that has whitespace removed from the start and the end, and which has each sequence of internal whitespace replaced with a single space. honestly obsessed, it's giving spring cleaning ๐Ÿงนโœจ

Whitespace means any character for which QChar::isSpace() returns TRUE. This includes Unicode characters with decimal values 9 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR), and 32 (Space).

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

See also stripWhiteSpace().

QString & QString::sprintf ( const char * cformat, ... )</p>

Okay bestie, this one builds you a formatted string from the format string cformat and a whole arbitrary list of arguments, and it does it safely ๐Ÿ’…. The format string supports literally every escape sequence of printf() from the standard C library, no cap.

The %s escape sequence expects a utf8() encoded string. The format string cformat is expected to be in latin1. If you need a Unicode format string, use arg() instead. And for typesafe string building with full Unicode support, you can use QTextOStream like this:

QString str;
QString s = ...;
int x = ...;
QTextOStream( &str ) << s << " : " << x;

For translations โ€” especially if the strings contain more than one escape sequence โ€” you should consider using the arg() function instead. That lets the order of the replacements be controlled by the translator, and it has Unicode support. Iconic behaviour honestly โœจ.

The %lc escape sequence expects a unicode character of type ushort (as returned by QChar::unicode()). The %ls escape sequence expects a pointer to a zero-terminated array of unicode characters of type ushort (as returned by QString::ucs2()).

See also arg().

Examples:

okay so the original page just... puts "Examples:" here and then nothing?? the vibes are immaculate but the content is not, we move on ๐Ÿซถ

void QString::squeeze ()

Squeezes the string's capacity down to the current content. Little glow-up, little wardrobe purge, she's taking up exactly as much space as she needs ๐Ÿ’–.

See also capacity() and reserve().


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

Returns TRUE if the string starts with s; otherwise returns FALSE. Fr fr, that's it.

If cs is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive.

QString str( "Bananas" );
str.startsWith( "Ban" );     // returns TRUE
str.startsWith( "Car" );     // returns FALSE

See also endsWith().


QString QString::stripWhiteSpace () const

Returns a string that has whitespace removed from the start and the end. She's setting boundaries, bestie ๐ŸŽ€.

Whitespace means any character for which QChar::isSpace() returns TRUE. This includes Unicode characters with decimal values 9 (TAB), 10 (LF), 11 (VT), 12 (FF), 13 (CR) and 32 (Space), and may also include other Unicode characters.

QString string = "   white space   ";
QString s = string.stripWhiteSpace();       // s == "white space"

See also simplifyWhiteSpace().


double QString::toDouble ( bool * ok = 0 ) const

Returns the string converted to a double value. Number go brrr ๐Ÿ”ฅ.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

QString string( "1234.56" );
double a = string.toDouble();   // a == 1234.56

The string-to-number functions:

  • toShort()
  • toUShort()
  • toInt()
  • toUInt()
  • toLong()
  • toULong()
  • toLongLong()
  • toULongLong()
  • toFloat()

toDouble() can handle numbers represented in various locales. These representations may use different characters for the decimal point, thousands group separator and even individual digits. QString's functions try to interpret the string according to the current locale. The current locale is determined from the system at application startup and can be changed by calling QLocale::setDefault(). If the string cannot be interpreted according to the current locale, this function falls back on the "C" locale.

bool ok;
double d;
QLocale::setDefault(QLocale::C);
d = QString( "1234,56" ).toDouble(&ok); // ok == false
d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
QLocale::setDefault(QLocale::German);
d = QString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56
d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56

Due to the ambiguity between the decimal point and thousands group separator in various locales, these functions do not handle thousands group separators. If you need to convert such numbers, use the corresponding function in QLocale.

bool ok;
QLocale::setDefault(QLocale::C);
double d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false
โš ๏ธ Warning: If the string contains trailing whitespace this function will fail, and set *ok to false if ok is not 0. Leading whitespace is ignored.

See also number(), QLocale::setDefault(), QLocale::toDouble(), and stripWhiteSpace().


float QString::toFloat ( bool * ok = 0 ) const

Returns the string converted to a float value.

Returns 0.0 if the conversion fails. Not her finest moment but she said what she said ๐Ÿ’€.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

For information on how string-to-number functions in QString handle localized input, see toDouble().

โš ๏ธ Warning: If the string contains trailing whitespace this function will fail, settings *ok to false if ok is not 0. Leading whitespace is ignored.

See also number().


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

Returns the string converted to an int using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

QString str( "FF" );
bool ok;
int hex = str.toInt( &ok, 16 );     // hex == 255, ok == TRUE
int dec = str.toInt( &ok, 10 );     // dec == 0, ok == FALSE

Leading and trailing whitespace is ignored by this function. (Yes, unlike toDouble() โ€” she's the chill one ๐Ÿ˜Œ.)

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().


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

Returns the string converted to a long using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

Leading and trailing whitespace is ignored by this function.

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().


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

Returns the string converted to a long long using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

Leading and trailing whitespace is ignored by this function.

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().


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

Returns the string converted to a short using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

Leading and trailing whitespace is ignored by this function.

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().


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

Returns the string converted to an unsigned int using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

Leading and trailing whitespace is ignored by this function.

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().


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

Returns the string converted to an unsigned long using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

Leading and trailing whitespace is ignored by this function.

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().


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

Returns the string converted to an unsigned long long using base base, which is 10 by default and must be between 2 and 36 or 0. If base is 0, the base is determined automatically using the following rules:

  • If the string begins with "0x", it is assumed to be hexadecimal;
  • If it begins with "0", it is assumed to be octal;
  • Otherwise it is assumed to be decimal.

Returns 0 if the conversion fails.

If ok is not 0: if a conversion error occurs, *ok is set to FALSE; otherwise *ok is set to TRUE.

Leading and trailing whitespace is ignored by this function.

For information on how string-to-number functions in QString handle localized input, see toDouble().

See also number().

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

okay so this one hands you back an unsigned short, converted using base base โ€” default is 10, and it has to be between 2 and 36, or 0. and if base is 0?? bestie the string just FIGURES IT OUT itself, using these rules:

  • if the string starts with "0x", it's assumed to be hexadecimal ๐Ÿ’œ
  • if it starts with "0", it's assumed to be octal
  • otherwise it's assumed to be decimal

returns 0 if the conversion flops. ๐Ÿ˜ญ

if ok isn't 0: on a conversion error *ok gets set to FALSE; otherwise *ok gets set to TRUE.

leading and trailing whitespace is ignored by this function, no cap.

and for the tea on how QString's string-to-number functions deal with localized input? go look at toDouble().

See also number().


void QString::truncate ( uint newLen )

if newLen is less than the length of the string, the string gets chopped at position newLen. otherwise?? literally nothing happens, it's giving chill. ๐Ÿ˜Œ

QString s = "truncate me";
s.truncate( 5 );            // s == "trunc"

See also setLength().

Example: network/mail/smtp.cpp.


const unsigned short * QString::ucs2 () const

returns the QString as a zero terminated array of unsigned shorts if the string isn't null; otherwise returns zero. ๐Ÿซถ

the result stays valid so long as one unmodified copy of the source string exists.

Example: dotnet/wrapper/lib/tools.cpp.


const QChar * QString::unicode () const

returns the Unicode representation of the string. the result stays valid until the string is modified โ€” so don't go editing it and then squinting at stale data, that's not the vibe. ๐Ÿ’…


QString QString::upper () const

returns an UPPERCASE copy of the string. main character energy only.

QString string( "TeXt" );
str = string.upper();     // t == "TEXT"

See also lower().

Examples:


QCString QString::utf8 () const

returns the string encoded in UTF-8 format. โœจ

See QTextCodec for more diverse coding/decoding of Unicode strings.

See also fromUtf8(), ascii(), latin1(), and local8Bit().

Example: network/archivesearch/archivedialog.ui.h.


bool operator!= ( const QString & s1, const QString & s2 )

returns TRUE if s1 is not equal to s2; otherwise returns FALSE. and listen โ€” a null string is not equal to a not-null empty string. they are NOT the same girl. ๐Ÿ™ˆ

equivalent to compare(s1, s2) != 0.

See also isNull() and isEmpty().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is not equal to s2; otherwise returns FALSE. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) != 0.

See also isNull() and isEmpty().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is not equal to s2; otherwise returns FALSE. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) != 0.

See also isNull() and isEmpty().


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

returns a string which is the result of concatenating the string s1 and the string s2. slay, glue them together bestie. ๐ŸŽ€

equivalent to s1.append(s2).


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns a string which is the result of concatenating the string s1 and character s2.

equivalent to s1.append(s2).


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns a string which is the result of concatenating the character s1 and string s2.


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns a string which is the result of concatenating the string s and character c.

equivalent to s.append(c).


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns a string which is the result of concatenating the character c and string s.

equivalent to s.prepend(c).


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

returns TRUE if s1 is lexically less than s2; otherwise returns FALSE. the comparison is case sensitive, ok? no sneaky case-blind behaviour here. ๐Ÿ‘‘

equivalent to compare(s1, s2) < 0.


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is lexically less than s2; otherwise returns FALSE. the comparison is case sensitive.

equivalent to compare(s1, s2) < 0.


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

writes the string str to the stream s. ๐Ÿ“ฎ

See also Format of the QDataStream operators


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

returns TRUE if s1 is lexically less than or equal to s2; otherwise returns FALSE. the comparison is case sensitive. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1,s2) <= 0.

See also isNull() and isEmpty().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is lexically less than or equal to s2; otherwise returns FALSE. the comparison is case sensitive. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) <= 0.

See also isNull() and isEmpty().


bool operator== ( const QString & s1, const QString & s2 )

returns TRUE if s1 is equal to s2; otherwise returns FALSE. note that a null string is not equal to a not-null empty string. (yes we're saying it again. it matters. ๐Ÿ’€)

equivalent to compare(s1, s2) == 0.

See also isNull() and isEmpty().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is equal to s2; otherwise returns FALSE. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) == 0.

See also isNull() and isEmpty().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is equal to s2; otherwise returns FALSE. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) == 0.

See also isNull() and isEmpty().


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

returns TRUE if s1 is lexically greater than s2; otherwise returns FALSE. the comparison is case sensitive.

equivalent to compare(s1, s2) > 0.


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is lexically greater than s2; otherwise returns FALSE. the comparison is case sensitive.

equivalent to compare(s1, s2) > 0.


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

returns TRUE if s1 is lexically greater than or equal to s2; otherwise returns FALSE. the comparison is case sensitive. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) >= 0.

See also isNull() and isEmpty().


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

this is an overloaded member function, provided for convenience. it behaves essentially like the above function.

returns TRUE if s1 is lexically greater than or equal to s2; otherwise returns FALSE. the comparison is case sensitive. note that a null string is not equal to a not-null empty string.

equivalent to compare(s1, s2) >= 0.

See also isNull() and isEmpty().


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

reads a string from the stream s into string str. ๐Ÿ“ฅ

See also Format of the QDataStream operators


๐ŸŒธ SEE ALSO ๐ŸŒธ


Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the license file included in the distribution for a complete license statement.


โœจ AUTHOR โœจ

Generated automatically from the source code. (no human author, just vibes and a code generator fr fr ๐Ÿค–)


๐Ÿ’€ BUGS ๐Ÿ’€

if you find a bug in Qt, please report it as described in http://doc.trolltech.com/bughowto.html. good bug reports help us to help you. thank you. ๐Ÿซถ

the definitive Qt documentation is provided in HTML format; it's located at $QTDIR/doc/html and can be read using Qt Assistant or with a web browser. this man page is provided as a convenience for those users who prefer man pages, although this format is not officially supported by Trolltech. ๐Ÿ˜ญ

if you find errors in this manual page, please report them to qt-bugs@trolltech.com. please include the name of the manual page (qstring.3qt) and the Qt version (3.3.8).


okay bestie that's the WHOLE page, we did it, we survived all nine parts ๐Ÿ’…โœจ this was Trolltech AS, dated 2 February 2007, QString(3qt) โ€” go forth and manipulate strings responsibly, mwah ๐Ÿ’–๐Ÿฆ‹๐ŸŽ€

๐Ÿ“ฆ where this page comes from