Q3CString Class

The Q3CString class provides an abstraction of the classic C zero-terminated char array (char *). 更多...

头: #include <Q3CString>
继承: QByteArray

注意: 此类的所有函数 可重入 .

公共函数

Q3CString ()
Q3CString (int size )
Q3CString (const Q3CString & s )
Q3CString (const QByteArray & ba )
Q3CString (const char * str )
Q3CString (const char * str , uint maxsize )
Q3CString & append (const char * str )
Q3CString copy () const
Q3CString leftJustify (uint width , char fill = ' ', bool truncate = false) const
Q3CString lower () const
Q3CString rightJustify (uint width , char fill = ' ', bool truncate = false) const
bool setExpand (uint index , char c )
Q3CString & setNum (double n , char f = 'g', int prec = 6)
Q3CString & setNum (long n )
Q3CString & setNum (ulong n )
Q3CString & setNum (int n )
Q3CString & setNum (uint n )
Q3CString & setNum (short n )
Q3CString & setNum (ushort n )
Q3CString & setNum (float n , char f = 'g', int prec = 6)
Q3CString & setStr (const char * str )
Q3CString simplifyWhiteSpace () const
Q3CString & sprintf (const char * format , ...)
Q3CString stripWhiteSpace () const
double toDouble (bool * ok = 0) const
float toFloat (bool * ok = 0) const
int toInt (bool * ok = 0) const
long toLong (bool * ok = 0) const
short toShort (bool * ok = 0) const
uint toUInt (bool * ok = 0) const
ulong toULong (bool * ok = 0) const
ushort toUShort (bool * ok = 0) const
Q3CString upper () const
Q3CString & operator= (const Q3CString & s )
Q3CString & operator= (const QByteArray & ba )
Q3CString & operator= (const char * str )
Q_COMPAT_EXPORT_INLINE operator!= (const Q3CString & s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE operator!= (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE operator!= (const char * s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE const operator+ (const Q3CString & s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE const operator+ (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE const operator+ (const char * s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE const operator+ (const Q3CString & s , char c )
Q_COMPAT_EXPORT_INLINE const operator+ (char c , const Q3CString & s )
Q_COMPAT_EXPORT_INLINE operator< (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE operator< (const char * s1 , const Q3CString & s2 )
QDataStream & operator<< (QDataStream & s , const Q3CString & str )
Q_COMPAT_EXPORT_INLINE operator<= (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE operator<= (const char * s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE operator== (const Q3CString & s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE operator== (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE operator== (const char * s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE operator> (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE operator> (const char * s1 , const Q3CString & s2 )
Q_COMPAT_EXPORT_INLINE operator>= (const Q3CString & s1 , const char * s2 )
Q_COMPAT_EXPORT_INLINE operator>= (const char * s1 , const Q3CString & s2 )
QDataStream & operator>> (QDataStream & s , Q3CString & str )

额外继承成员

详细描述

The Q3CString class provides an abstraction of the classic C zero-terminated char array (char *).

Q3CString tries to behave like a more convenient const char * . The price of doing this is that some algorithms will perform badly. For example, append () is O( length ()) since it scans for a null terminator. Although you might use Q3CString for text that is never exposed to the user, for most purposes, and especially for user-visible text, you should use QString . QString provides implicit sharing, Unicode and other internationalization support, and is well optimized.

Note that for the Q3CString methods that take a const char * parameter the const char * must either be 0 (null) or not-null and '\0' (NUL byte) terminated; otherwise the results are undefined.

A default constructed Q3CString is null , i.e. both the length and the data pointer are 0 and isNull () returns true.

注意: However, if you ask for the data pointer of a null Q3CString 通过调用 data (), then because the internal representation of the null Q3CString is shared, it will be detached and replaced with a non-shared, empty representation, a non-null data pointer will be returned, and subsequent calls to isNull () will return false. But if you ask for the data pointer of a null Q3CString 通过调用 constData (), the shared internal representation is not detached, a null data pointer is returned, and subsequent calls to isNull () will continue to return true.

A Q3CString that references the empty string ("", a single '\0' char) is empty , i.e. isEmpty () returns true. Both null and empty Q3CStrings are legal parameters to the methods. Assigning const char * 0 to Q3CString produces a null Q3CString .

The length () function returns the length of the string; resize () resizes the string and truncate () truncates the string. A string can be filled with a character using fill (). Strings can be left or right padded with characters using leftJustify () 和 rightJustify (). Characters, strings and regular expressions can be searched for using find () 和 findRev (), and counted using contains ().

Strings and characters can be inserted with insert() and appended with append (). A string can be prepended with prepend(). Characters can be removed from the string with remove() and replaced with replace().

Portions of a string can be extracted using left(), right() and mid(). Whitespace can be removed using stripWhiteSpace () 和 simplifyWhiteSpace (). Strings can be converted to uppercase or lowercase with upper () 和 lower () 分别。

Strings that contain numbers can be converted to numbers with toShort (), toInt (), toLong (), toULong (), toFloat () 和 toDouble (). Numbers can be converted to strings with setNum ().

Many operators are overloaded to work with Q3CStrings. Q3CString also supports some more obscure functions, e.g. sprintf (), setStr () 和 setExpand ().

Note on Character Comparisons

Q3CString the notion of uppercase and lowercase and of which character is greater than or less than another character is locale dependent. This affects functions which support a case insensitive option or which compare or lowercase or uppercase their arguments. Case insensitive operations and comparisons will be accurate if both strings contain only ASCII characters. (If $LC_CTYPE is set, most Unix systems do "the right thing".) Functions that this affects include contains (), find (), findRev (), operator< (), operator<= (), operator> (), operator>= (), lower () 和 upper ().

This issue does not apply to QString s since they represent characters using Unicode.

Performance note: The Q3CString methods for QRegExp searching are implemented by converting the Q3CString QString and performing the search on that. This implies a deep copy of the Q3CString data. If you are going to perform many QRegExp searches on a large Q3CString , you will get better performance by converting the Q3CString QString yourself, and then searching in the QString .

成员函数文档编制

Q3CString:: Q3CString ()

Constructs a null string.

另请参阅 isNull ().

Q3CString:: Q3CString ( int size )

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

size > 0, then the first and last characters in the string are initialized to '\0'. All other characters are uninitialized.

另请参阅 resize () 和 isNull ().

Q3CString:: Q3CString (const Q3CString & s )

Constructs a shallow copy s .

Q3CString:: Q3CString (const QByteArray & ba )

构造副本为 ba .

Q3CString:: Q3CString (const char * str )

Constructs a string that is a deep copy of str .

str is 0 a null string is created.

另请参阅 isNull ().

Q3CString:: Q3CString (const char * str , uint maxsize )

Constructs a string that is a deep copy of str . The copy will be at most maxsize bytes long including the '\0'-terminator.

范例:

Q3CString str("helloworld", 6); // assigns "hello" to str
					

str contains a 0 byte within the first maxsize bytes, the resulting Q3CString will be terminated by this 0. If str is 0 a null string is created.

另请参阅 isNull ().

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

Appends string str to the string and returns a reference to the string. Equivalent to operator+=().

Q3CString Q3CString:: copy () const

Returns a deep copy of this string.

Q3CString Q3CString:: leftJustify ( uint width , char fill = ' ', bool truncate = false) const

Returns a string of length width (plus one for the terminating '\0') that contains this string padded with the fill character.

If the length of the string exceeds width and truncate is false (the default), then the returned string is a copy of the string. If the length of the string exceeds width and truncate is true, then the returned string is a left( width ).

范例:

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

另请参阅 rightJustify ().

Q3CString Q3CString:: lower () const

使用 QByteArray::toLower () 代替。

Q3CString Q3CString:: rightJustify ( uint width , char fill = ' ', bool truncate = false) const

Returns a string of length width (plus one for the terminating '\0') that contains zero or more of the fill character followed by this string.

If the length of the string exceeds width and truncate is false (the default), then the returned string is a copy of the string. If the length of the string exceeds width and truncate is true, then the returned string is a left( width ).

范例:

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

另请参阅 leftJustify ().

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

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

Returns false if index was out of range and the string could not be expanded; otherwise returns true.

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

Sets the string to the string representation of the number n and returns a reference to the string.

The format of the string representation is specified by the format character f , and the precision (number of digits after the decimal point) is specified with prec .

The valid formats for f are 'e', 'E', 'f', 'g' and 'G'. The formats are the same as for sprintf (); they are explained in QString::arg ().

Q3CString & Q3CString:: setNum ( long n )

这是重载函数。

Sets the string to the string representation of the number n and returns a reference to the string.

Q3CString & Q3CString:: setNum ( ulong n )

这是重载函数。

Sets the string to the string representation of the number n and returns a reference to the string.

Q3CString & Q3CString:: setNum ( int n )

这是重载函数。

Sets the string to the string representation of the number n and returns a reference to the string.

Q3CString & Q3CString:: setNum ( uint n )

这是重载函数。

Sets the string to the string representation of the number n and returns a reference to the string.

Q3CString & Q3CString:: setNum ( short n )

这是重载函数。

Sets the string to the string representation of the number n and returns a reference to the string.

Q3CString & Q3CString:: setNum ( ushort n )

这是重载函数。

Sets the string to the string representation of the number n and returns a reference to the string.

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

这是重载函数。

Q3CString & Q3CString:: setStr (const char * str )

Makes a deep copy of str . Returns a reference to the string.

Q3CString Q3CString:: simplifyWhiteSpace () const

使用 QByteArray::simplified () 代替。

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

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

If the string is shorter than 256 characters, this sprintf() calls resize(256) to decrease the chance of memory corruption. The string is resized back to its actual length before sprintf() returns.

范例:

Q3CString s;
s.sprintf("%d - %s", 1, "first");                // result < 256 chars
Q3CString big(25000);                        // very long string
big.sprintf("%d - %s", 2, longString);        // result < 25000 chars
					

警告: All vsprintf() implementations will write past the end of the target string (*this) if the format specification and arguments happen to be longer than the target string, and some will also fail if the target string is longer than some arbitrary implementation limit.

Giving user-supplied arguments to sprintf() is risky: Sooner or later someone will paste a huge line into your application.

Q3CString Q3CString:: stripWhiteSpace () const

使用 QByteArray::trimmed () 代替。

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

Returns the string converted to a double 值。

ok is not 0: * ok is set to false if the string is not a number, or if it has trailing garbage; otherwise * ok is set to true.

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

Returns the string converted to a float 值。

ok is not 0: * ok is set to false if the string is not a number, or if it has trailing garbage; otherwise * ok is set to true.

int Q3CString:: toInt ( bool * ok = 0) const

Returns the string converted to a int 值。

ok is not 0: * ok is set to false if the string is not a number, or if it has trailing garbage; otherwise * ok is set to true.

long Q3CString:: toLong ( bool * ok = 0) const

Returns the string converted to a long 值。

ok is not 0: * ok is set to false if the string is not a number, or if it has trailing garbage; otherwise * ok is set to true.

short Q3CString:: toShort ( bool * ok = 0) const

Returns the string converted to a short 值。

ok is not 0: * ok is set to false if the string is not a number, is out of range, or if it has trailing garbage; otherwise * ok is set to true.

uint Q3CString:: toUInt ( bool * ok = 0) const

Returns the string converted to an 无符号 int 值。

ok is not 0: * ok is set to false if the string is not a number, or if it has trailing garbage; otherwise * ok is set to true.

ulong Q3CString:: toULong ( bool * ok = 0) const

Returns the string converted to an unsigned long 值。

ok is not 0: * ok is set to false if the string is not a number, or if it has trailing garbage; otherwise * ok is set to true.

ushort Q3CString:: toUShort ( bool * ok = 0) const

Returns the string converted to an unsigned short 值。

ok is not 0: * ok is set to false if the string is not a number, is out of range, or if it has trailing garbage; otherwise * ok is set to true.

Q3CString Q3CString:: upper () const

使用 QByteArray::toUpper () 代替。

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

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

Q3CString & Q3CString:: operator= (const QByteArray & ba )

Assigns byte array ba 到此 Q3CString .

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

这是重载函数。

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

str is 0 a null string is created.

另请参阅 isNull ().

相关非成员

Q_COMPAT_EXPORT_INLINE operator!= (const Q3CString & s1 , const Q3CString & s2 )

返回 true 若 s1 and s2 are different; otherwise returns false.

Equivalent to qstrcmp( s1 , s2 ) != 0.

Q_COMPAT_EXPORT_INLINE operator!= (const Q3CString & s1 , const char * s2 )

这是重载函数。

返回 true 若 s1 and s2 are different; otherwise returns false.

Equivalent to qstrcmp( s1 , s2 ) != 0.

Q_COMPAT_EXPORT_INLINE operator!= (const char * s1 , const Q3CString & s2 )

这是重载函数。

返回 true 若 s1 and s2 are different; otherwise returns false.

Equivalent to qstrcmp( s1 , s2 ) != 0.

Q_COMPAT_EXPORT_INLINE const operator+ (const Q3CString & s1 , const Q3CString & s2 )

Returns a string which consists of the concatenation of s1 and s2 .

Q_COMPAT_EXPORT_INLINE const operator+ (const Q3CString & s1 , const char * s2 )

这是重载函数。

Returns a string which consists of the concatenation of s1 and s2 .

Q_COMPAT_EXPORT_INLINE const operator+ (const char * s1 , const Q3CString & s2 )

这是重载函数。

Returns a string which consists of the concatenation of s1 and s2 .

Q_COMPAT_EXPORT_INLINE const operator+ (const Q3CString & s , char c )

这是重载函数。

Returns a string which consists of the concatenation of s and c .

Q_COMPAT_EXPORT_INLINE const operator+ ( char c , const Q3CString & s )

这是重载函数。

Returns a string which consists of the concatenation of c and s .

Q_COMPAT_EXPORT_INLINE operator< (const Q3CString & s1 , const char * s2 )

返回 true 若 s1 小于 s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) < 0.

Q_COMPAT_EXPORT_INLINE operator< (const char * s1 , const Q3CString & s2 )

这是重载函数。

返回 true 若 s1 小于 s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) < 0.

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

Writes string str 到流 s .

另请参阅 QDataStream 运算符格式 .

Q_COMPAT_EXPORT_INLINE operator<= (const Q3CString & s1 , const char * s2 )

返回 true 若 s1 <= s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) <= 0.

Q_COMPAT_EXPORT_INLINE operator<= (const char * s1 , const Q3CString & s2 )

这是重载函数。

返回 true 若 s1 <= s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) <= 0.

Q_COMPAT_EXPORT_INLINE operator== (const Q3CString & s1 , const Q3CString & s2 )

返回 true 若 s1 and s2 相等;否则返回 false。

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

Q_COMPAT_EXPORT_INLINE operator== (const Q3CString & s1 , const char * s2 )

这是重载函数。

返回 true 若 s1 and s2 相等;否则返回 false。

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

Q_COMPAT_EXPORT_INLINE operator== (const char * s1 , const Q3CString & s2 )

这是重载函数。

返回 true 若 s1 and s2 相等;否则返回 false。

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

Q_COMPAT_EXPORT_INLINE operator> (const Q3CString & s1 , const char * s2 )

返回 true 若 s1 大于 s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) > 0.

Q_COMPAT_EXPORT_INLINE operator> (const char * s1 , const Q3CString & s2 )

这是重载函数。

返回 true 若 s1 大于 s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) > 0.

Q_COMPAT_EXPORT_INLINE operator>= (const Q3CString & s1 , const char * s2 )

返回 true 若 s1 >= s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) >= 0.

Q_COMPAT_EXPORT_INLINE operator>= (const char * s1 , const Q3CString & s2 )

这是重载函数。

返回 true 若 s1 >= s2 ;否则返回 false。

Equivalent to qstrcmp( s1 , s2 ) >= 0.

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

Reads a string into str 从流 s .

另请参阅 QDataStream 运算符格式 .