Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQString Class Reference

The TQString class provides an abstraction of Unicode text and the classic C '\0'-terminated char array. More...

All the functions in this class are reentrant when TQt is built with thread support.

#include <tqstring.h>

List of all member functions.

Public Members

Static Public Members

Related Functions


Detailed Description

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

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

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

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

Note that if you find that you are mixing usage of TQCString, TQString, and TQByteArray, 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 TQCString; if it is unterminated (i.e. contains '\0's) 8-bit data, use TQByteArray; if it is text, use TQString.

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

Note for C programmers

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

    TQString boolToString( bool b )
    {
        TQString 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 TQt's source code you will encounter TQString usages like this:

    TQString func( const TQString& input )
    {
        TQString 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. TQString (like all TQt's implicitly shared classes) operates on a copy-on-write basis, only copying if an instance is actually changed.

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

See also TQChar, TQCString, TQByteArray, TQConstString, Implicitly and Explicitly Shared Classes, Text Related Classes, and Non-GUI Classes.


Member Type Documentation

TQString::SectionFlags

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

See also section().


Member Function Documentation

TQString::TQString ()

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

See also isNull().

TQString::TQString ( TQChar ch )

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

TQString::TQString ( const TQString & s )

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

TQString::TQString ( const TQByteArray & ba )

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

TQString::TQString ( const TQChar * unicode, uint length )

Constructs a string that is a deep copy of the first length characters in the TQChar 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: TQString 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().

TQString::TQString ( 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 TQTextCodec::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 TQString preserves all the information. You can disable this constructor by defining TQT_NO_CAST_ASCII when you compile your applications. You can also make TQString objects by using setLatin1(), fromLatin1(), fromLocal8Bit(), and fromUtf8(). Or whatever encoding is appropriate for the 8-bit data you have.

See also isNull() and fromAscii().

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

Constructs a string that is a deep copy of str.

This is the same as fromAscii(str).

TQString::~TQString ()

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

TQString & TQString::append ( const TQString & 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.

TQString & TQString::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+=().

TQString & TQString::append ( TQChar 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+=().

TQString & TQString::append ( const TQByteArray & 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+=().

TQString & TQString::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+=().

TQString & TQString::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+=().

TQString TQString::arg ( const TQString & 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:

    TQString status = TQString( "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 (tqWarning()) is output and the result is undefined.

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

TQString TQString::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 TQLocale::setDefault(). The 'L' flag is ignored if base is not 10.

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

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

TQString TQString::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.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

a 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.

TQString TQString::arg ( ushort 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.

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

This is an overloaded member function, provided for convenience. It 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:

Format Meaning
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g use e or f format, whichever is the most concise
G use E or f format, whichever is the most concise

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;
        TQString ds = TQString( "'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.

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

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

a is assumed to be in the Latin-1 character set.

TQString TQString::arg ( TQChar a, int fieldWidth = 0 ) const

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

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

This is an overloaded member function, provided for convenience. It 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. This can make a difference if a1 contains e.g. %1:

    TQString 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"
    

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

This is an overloaded member function, provided for convenience. It 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.

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

This is an overloaded member function, provided for convenience. It 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 * TQString::ascii () const

Returns an 8-bit ASCII representation of the string.

If a codec has been set using TQTextCodec::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.

TQChar TQString::at ( uint i ) const

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

        const TQString string( "abcdefgh" );
        TQChar ch = string.at( 4 );
        // ch == 'e'
    

If the TQString is not const (i.e. const TQString) or const& (i.e. const TQString &), then the non-const overload of at() will be used instead.

TQCharRef TQString::at ( uint 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.

If i is beyond the length of the string then the string is expanded with TQChar::null.

uint TQString::capacity () const

Returns the number of characters this string can hold in the allocated memory.

See also reserve() and squeeze().

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

Lexically compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

The comparison is based exclusively on the numeric Unicode values of the characters and is very fast, but is not what a human would expect. Consider sorting user-interface strings with TQString::localeAwareCompare().

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

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

This is an overloaded member function, provided for convenience. It 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 TQString::compose ()

Warning: This function is not supported in TQt 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 TQString. Useful when composition-rich text requires rendering with glyph-poor fonts, but it also makes compositions such as TQChar(0x0041) ('A') and TQChar(0x0308) (Unicode accent diaresis), giving TQChar(0x00c4) (German A Umlaut).

TQChar TQString::constref ( uint i ) const

Returns the TQChar at index i by value.

Equivalent to at(i).

See also ref().

int TQString::contains ( TQChar 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.

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

Examples: fileiconview/tqfileiconview.cpp and mdi/application.cpp.

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

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

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

This is an overloaded member function, provided for convenience. It 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 TQString::contains ( const TQString & str, bool cs = TRUE ) const

This is an overloaded member function, provided for convenience. It 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".

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

See also findRev().

int TQString::contains ( const TQRegExp & rx ) const

This is an overloaded member function, provided for convenience. It 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".

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

See also find() and findRev().

TQString TQString::copy () const

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

In TQt 2.0 and later, all calls to this function are needless. Just remove them.

const char * TQString::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 TQt 1.x, this returned a char* allowing direct manipulation of the string as a sequence of bytes. In TQt 2.x where TQString is a Unicode string, char* conversion constructs a temporary string, and hence direct character operations are meaningless.

bool TQString::endsWith ( const TQString & 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.

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

See also startsWith().

Example: chart/main.cpp.

TQString & TQString::fill ( TQChar 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.

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

int TQString::find ( const TQRegExp & rx, int index = 0 ) const

Finds the first match of the regular expression rx, starting from position index. If index is -1, the search starts at the last character; if -2, at the next to last character and so on. (See findRev() for searching backwards.)

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

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

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

Example: network/mail/smtp.cpp.

int TQString::find ( TQChar 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. (See findRev() for searching backwards.)

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

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

int TQString::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 the search is case insensitive.

int TQString::find ( const TQString & 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 the search is case insensitive.

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

int TQString::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(TQString(str), index).

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

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

int TQString::findRev ( TQChar 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. 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 the search is case insensitive.

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

int TQString::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 the search is case insensitive.

int TQString::findRev ( const TQString & 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 the search is case insensitive.

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

int TQString::findRev ( const TQRegExp & 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.)

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

        TQString string( "bananas" );
        int i = string.findRev( TQRegExp("an") );      // i == 3
    

See also find().

TQString TQString::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. If len is bigger than the length of ascii then it will use the length of ascii.

If a codec has been set using TQTextCodec::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 TQString(const char*) constructor, but you can make that constructor invisible if you compile with the define TQT_NO_CAST_ASCII, in which case you can explicitly create a TQString from 8-bit ASCII text using this function.

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

TQString TQString::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: listbox/listbox.cpp and network/mail/smtp.cpp.

TQString TQString::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.

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

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

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

TQString TQString::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.

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

See also isNull().

TQString TQString::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.

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

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

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

TQString & TQString::insert ( uint index, const TQString & 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 returns a reference to the string.

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

See also remove() and replace().

Examples: themes/themes.cpp and xform/xform.cpp.

TQString & TQString::insert ( uint index, const TQByteArray & 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.

TQString & TQString::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.

TQString & TQString::insert ( uint index, const TQChar * 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.

TQString & TQString::insert ( uint index, TQChar 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.

TQString & TQString::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 TQString::isEmpty () const

Returns TRUE if the string is empty, i.e. if length() == 0; otherwise returns FALSE. Null strings are also empty.

        TQString a( "" );
        a.isEmpty();        // TRUE
        a.isNull();         // FALSE

        TQString b;
        b.isEmpty();        // TRUE
        b.isNull();         // TRUE
    

See also isNull() and length().

Examples: addressbook/mainwindow.cpp, chart/chartform.cpp, chart/chartform_canvas.cpp, network/networkprotocol/nntp.cpp, qmag/qmag.cpp, and qwerty/qwerty.cpp.

bool TQString::isNull () const

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

        TQString 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: i18n/main.cpp, network/ftpclient/ftpmainwindow.ui.h, and tqdir/tqdir.cpp.

const char * TQString::latin1 () const

Returns a Latin-1 representation of the string. The returned value is undefined if the string contains non-Latin-1 characters. If you want to convert strings into formats other than Unicode, see the TQTextCodec classes.

This function is mainly useful for boot-strapping legacy code to use Unicode.

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

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

Examples: fileiconview/tqfileiconview.cpp and network/networkprotocol/nntp.cpp.

TQString TQString::left ( uint len ) const

Returns a substring that contains the len leftmost characters of the string.

The whole string is returned if len exceeds the length of the string.

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

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

Example: themes/themes.cpp.

TQString TQString::leftJustify ( uint width, TQChar fill = ' ', bool truncate = FALSE ) const

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

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 any characters in a copy of the string after length width are removed, and the copy is returned.

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

See also rightJustify().

uint TQString::length () const

Returns the length of the string.

Null strings and empty strings have zero length.

See also isNull() and isEmpty().

Examples: dirview/dirview.cpp, fileiconview/tqfileiconview.cpp, network/networkprotocol/nntp.cpp, rot13/rot13.cpp, and themes/themes.cpp.

TQCString TQString::local8Bit () const

Returns the string encoded in a locale-specific format. On X11, this is the TQTextCodec::codecForLocale(). On Windows, it is a system-defined encoding. On Mac OS X, this always uses UTF-8 as the encoding.

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

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

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

Compares s1 with s2 and returns an integer less than, equal to, or greater than zero if s1 is less than, equal to, or greater than s2.

The comparison is performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user.

See also TQString::compare() and TQTextCodec::locale().

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

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

Compares this string with s.

TQString TQString::lower () const

Returns a lowercase copy of the string.

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

See also upper().

Example: scribble/scribble.cpp.

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

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

Returns a null string if the string is empty or index is out of range. Returns the whole string from index if index + len exceeds the length of the string.

        TQString s( "Five pineapples" );
        TQString t = s.mid( 5, 4 );                  // t == "pine"
    

See also left() and right().

Examples: network/mail/smtp.cpp, qmag/qmag.cpp, and themes/themes.cpp.

TQString TQString::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.

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

See also setNum().

Examples: application/application.cpp, chart/chartform.cpp, fonts/simple-tqfont-demo/viewer.cpp, helpviewer/helpwindow.cpp, mdi/application.cpp, regexptester/regexptester.cpp, and sql/overview/extract/main.cpp.

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

TQString TQString::number ( TQ_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().

TQString TQString::number ( TQ_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().

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

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

TQString TQString::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:

Format Meaning
e format as [-]9.9e[+|-]999
E format as [-]9.9E[+|-]999
f format as [-]9.9
g use e or f format, whichever is the most concise
G use E or f format, whichever is the most concise

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;
    TQString ds = TQString( "'E' format, precision 3, gives %1" )
                    .arg( d, 0, 'E', 3 );
    // ds == "1.234E+001"
    

See also setNum().

TQString::operator const char * () const

Returns ascii(). Be sure to see the warnings documented in the ascii() function. Note that for new code which you wish to be strictly Unicode-clean, you can define the macro TQT_NO_ASCII_CAST when compiling your code to hide this function so that automatic casts are not done. This has the added advantage that you catch the programming error described in operator!().

TQString::operator std::string () const

Returns ascii() as a std::string.

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

bool TQString::operator! () const

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

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

Note that if you say

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

It will call "operator const char*()", which is inefficent; you may wish to define the macro TQT_NO_ASCII_CAST when writing code which you wish to remain Unicode-clean.

When you want the above semantics, use:

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

See also isEmpty().

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

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

TQString & TQString::operator+= ( const TQByteArray & 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.

TQString & TQString::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.

TQString & TQString::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.

TQString & TQString::operator+= ( TQChar 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.

TQString & TQString::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.

TQString & TQString::operator= ( TQChar c )

Sets the string to contain just the single character c.

TQString & TQString::operator= ( const TQString & 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.

TQString & TQString::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().

TQString & TQString::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.

TQString & TQString::operator= ( const TQCString & 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.

TQString & TQString::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.

TQChar TQString::operator[] ( int i ) const

Returns the character at index i, or TQChar::null if i is beyond the length of the string.

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

TQCharRef TQString::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.

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

The TQCharRef internal class can be used much like a constant TQChar, but if you assign to it, you change the original string (which will detach itself because of TQString's copy-on-write semantics). You will get compilation errors if you try to use the result as anything but a TQChar.

TQString & TQString::prepend ( const TQString & s )

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

Equivalent to insert(0, s).

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

See also insert().

TQString & TQString::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().

TQString & TQString::prepend ( TQChar 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().

TQString & TQString::prepend ( const TQByteArray & s )

This is an overloaded member function, provided for convenience. It 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().

TQString & TQString::prepend ( const char * s )

This is an overloaded member function, provided for convenience. It 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().

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

This is an overloaded member function, provided for convenience. It 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().

TQChar & TQString::ref ( uint i )

Returns the TQChar at index i by reference, expanding the string with TQChar::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.

        TQString string("ABCDEF");
        TQChar ch = string.ref( 3 );         // ch == 'D'
    

See also constref().

TQString & TQString::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.

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

See also insert() and replace().

TQString & TQString::remove ( const TQString & str, bool cs = TRUE )

This is an overloaded member function, provided for convenience. It 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).

TQString & TQString::remove ( TQChar c )

This is an overloaded member function, provided for convenience. It 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, "").

TQString & TQString::remove ( char c )

This is an overloaded member function, provided for convenience. It 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, "").

TQString & TQString::remove ( const char * str )

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

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

TQString & TQString::remove ( const TQRegExp & rx )

This is an overloaded member function, provided for convenience. It 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, "").

TQString & TQString::replace ( uint index, uint len, const TQString & 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.

        TQString string( "Say yes!" );
        string = string.replace( 4, 3, "NO" );
        // string == "Say NO!"
    

Warning: TQt 3.3.3 and earlier had different semantics for the case index >= length(), which contradicted the documentation. To avoid portability problems between TQt 3 versions and with TQt 4, we recommend that you never call the function with index >= length().

See also insert() and remove().

Examples: listviews/listviews.cpp, network/networkprotocol/nntp.cpp, qmag/qmag.cpp, and regexptester/regexptester.cpp.

TQString & TQString::replace ( uint index, uint len, const TQChar * 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 TQChar data from s, starting at position index, and returns a reference to the string.

See also insert() and remove().

TQString & TQString::replace ( uint index, uint len, TQChar c )

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

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

TQString & TQString::replace ( uint index, uint len, char c )

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

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

TQString & TQString::replace ( TQChar c, const TQString & after, bool cs = TRUE )

This is an overloaded member function, provided for convenience. It 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:

    TQString s = "a,b,c";
    s.replace( TQChar(','), " or " );
    // s == "a or b or c"
    

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

This is an overloaded member function, provided for convenience. It 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.

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

This is an overloaded member function, provided for convenience. It 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:

    TQString s = "Greek is Greek";
    s.replace( "Greek", "English" );
    // s == "English is English"
    

TQString & TQString::replace ( const TQRegExp & rx, const TQString & after )

This is an overloaded member function, provided for convenience. It 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:

    TQString s = "banana";
    s.replace( TQRegExp("an"), "" );
    // s == "ba"
  

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

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

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

TQString & TQString::replace ( TQChar c1, TQChar c2 )

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

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

void TQString::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:

        TQString 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.

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

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

TQString TQString::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.

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

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

Example: fileiconview/tqfileiconview.cpp.

TQString TQString::rightJustify ( uint width, TQChar 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.

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

See also leftJustify().

TQString TQString::section ( TQChar sep, int start, int end = 0xffffffff, int flags = SectionDefault ) const

This function returns a section of the string.

This string is treated as a sequence of fields separated by the character, 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.

    TQString csv( "forename,middlename,surname,phone" );
    TQString s = csv.section( ',', 2, 2 );   // s == "surname"

    TQString path( "/usr/local/bin/myapp" ); // First field is empty
    TQString s = path.section( '/', 3, 4 );  // s == "bin/myapp"
    TQString 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.

    TQString csv( "forename,middlename,surname,phone" );
    TQString s = csv.section( ',', -3, -2 );  // s == "middlename,surname"

    TQString path( "/usr/local/bin/myapp" ); // First field is empty
    TQString s = path.section( '/', -1 ); // s == "myapp"
    

See also TQStringList::split().

Examples: chart/element.cpp and network/ftpclient/ftpmainwindow.ui.h.

TQString TQString::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.

TQString TQString::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.

TQString TQString::section ( const TQString & 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.

    TQString data( "forename**middlename**surname**phone" );
    TQString 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.

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

See also TQStringList::split().

TQString TQString::section ( const TQRegExp & 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.

    TQString line( "forename\tmiddlename  surname \t \t phone" );
    TQRegExp sep( "\s+" );
    TQString 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.

    TQString line( "forename\tmiddlename  surname \t \t phone" );
    TQRegExp sep( "\\s+" );
    TQString s = line.section( sep, -3, -2 ); // s == "middlename  surname"
    

Warning: Using this TQRegExp version is much more expensive than the overloaded string and character versions.

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

TQString & TQString::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 TQString::setExpand ( uint index, TQChar 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 TQt 3.x, because operator[] will expand the string as necessary.

TQString & TQString::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 TQString::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.

See also reserve() and truncate().

TQString & TQString::setNum ( TQ_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.

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

TQString & TQString::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.

TQString & TQString::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.

TQString & TQString::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.

TQString & TQString::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.

TQString & TQString::setNum ( long n, int base = 10 )

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

TQString & TQString::setNum ( ulong n, int base = 10 )

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

TQString & TQString::setNum ( TQ_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.

TQString & TQString::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.

TQString & TQString::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.

TQString & TQString::setUnicode ( const TQChar * 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().

TQString & TQString::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().

TQString TQString::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.

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

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

See also stripWhiteSpace().

TQString & TQString::sprintf ( const char * cformat, ... )

Safely builds a formatted string from the format string cformat and an arbitrary list of arguments. The format string supports all the escape sequences of printf() in the standard C library.

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. For typesafe string building, with full Unicode support, you can use TQTextOStream like this:

        TQString str;
        TQString s = ...;
        int x = ...;
        TQTextOStream( &str ) << s << " : " << x;
    

For translations, especially if the strings contains more than one escape sequence, you should consider using the arg() function instead. This allows the order of the replacements to be controlled by the translator, and has Unicode support.

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

See also arg().

Examples: dclock/dclock.cpp, forever/forever.cpp, layout/layout.cpp, qmag/qmag.cpp, scrollview/scrollview.cpp, tooltip/tooltip.cpp, and xform/xform.cpp.

void TQString::squeeze ()

Squeezes the string's capacity to the current content.

See also capacity() and reserve().

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

Returns TRUE if the string starts with s; otherwise returns FALSE.

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

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

See also endsWith().

TQString TQString::stripWhiteSpace () const

Returns a string that has whitespace removed from the start and the end.

Whitespace means any character for which TQChar::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.

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

See also simplifyWhiteSpace().

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

Returns the string converted to a double value.

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

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

The string-to-number functions:

can handle numbers represented in various locales. These representations may use different characters for the decimal point, thousands group sepearator and even individual digits. TQString'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 TQLocale::setDefault(). If the string cannot be interpreted according to the current locale, this function falls back on the "C" locale.

        bool ok;
        double d;

        TQLocale::setDefault(TQLocale::C);
        d = TQString( "1234,56" ).toDouble(&ok); // ok == false
        d = TQString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56

        TQLocale::setDefault(TQLocale::German);
        d = TQString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56
        d = TQString( "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 TQLocale.

        bool ok;
        TQLocale::setDefault(TQLocale::C);
        double d = TQString( "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(), TQLocale::setDefault(), TQLocale::toDouble(), and stripWhiteSpace().

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

Returns the string converted to a float value.

Returns 0.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.

For information on how string-to-number functions in TQString 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 TQString::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:

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.

        TQString 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.

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

See also number().

long TQString::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:

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 TQString handle localized input, see toDouble().

See also number().

TQ_LLONG TQString::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:

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 TQString handle localized input, see toDouble().

See also number().

short TQString::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:

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 TQString handle localized input, see toDouble().

See also number().

uint TQString::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:

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 TQString handle localized input, see toDouble().

See also number().

ulong TQString::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:

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 TQString handle localized input, see toDouble().

See also number().

TQ_ULLONG TQString::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:

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 TQString handle localized input, see toDouble().

See also number().

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

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

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 TQString handle localized input, see toDouble().

See also number().

void TQString::truncate ( uint newLen )

If newLen is less than the length of the string, then the string is truncated at position newLen. Otherwise nothing happens.

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

See also setLength().

Example: network/mail/smtp.cpp.

const unsigned short * TQString::ucs2 () const

Returns the TQString as a zero terminated array of unsigned shorts if the string is not null; otherwise returns zero.

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

const TQChar * TQString::unicode () const

Returns the Unicode representation of the string. The result remains valid until the string is modified.

TQString TQString::upper () const

Returns an uppercase copy of the string.

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

See also lower().

Examples: scribble/scribble.cpp and sql/overview/custom1/main.cpp.

TQCString TQString::utf8 () const

Returns the string encoded in UTF-8 format.

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

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

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


Related Functions

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

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 TQString & 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 TQString & 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 TQString operator+ ( const TQString & s1, const TQString & s2 )

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

Equivalent to s1.append(s2).

const TQString operator+ ( const TQString & 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 TQString operator+ ( const char * s1, const TQString & 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 TQString operator+ ( const TQString & 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 TQString operator+ ( char c, const TQString & 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 TQString & s1, const char * s2 )

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

Equivalent to compare(s1, s2) < 0.

bool operator< ( const char * s1, const TQString & 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.

TQDataStream & operator<< ( TQDataStream & s, const TQString & str )

Writes the string str to the stream s.

See also Format of the TQDataStream operators

bool operator<= ( const TQString & 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 TQString & 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 TQString & s1, const TQString & 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.

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

See also isNull() and isEmpty().

bool operator== ( const TQString & 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 TQString & 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 TQString & 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 TQString & 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 TQString & 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 TQString & 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().

TQDataStream & operator>> ( TQDataStream & s, TQString & str )

Reads a string from the stream s into string str.

See also Format of the TQDataStream operators


This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8