Movatterモバイル変換


[0]ホーム

URL:


We bake cookies in your browser for a better experience. Using this site means that you consent.Read More

Menu

Qt Documentation

  • Qt 4.8
  • Qt3SupportLight
  • Q3CString

Q3CString Class

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

Header:#include <Q3CString>
Inherits:QByteArray

Note: All functions in this class arereentrant.

Public Functions

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)
Q3CStringcopy() const
Q3CStringleftJustify(uint width, char fill = ' ', bool truncate = false) const
Q3CStringlower() const
Q3CStringrightJustify(uint width, char fill = ' ', bool truncate = false) const
boolsetExpand(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)
Q3CStringsimplifyWhiteSpace() const
Q3CString &sprintf(const char * format, ...)
Q3CStringstripWhiteSpace() const
doubletoDouble(bool * ok = 0) const
floattoFloat(bool * ok = 0) const
inttoInt(bool * ok = 0) const
longtoLong(bool * ok = 0) const
shorttoShort(bool * ok = 0) const
uinttoUInt(bool * ok = 0) const
ulongtoULong(bool * ok = 0) const
ushorttoUShort(bool * ok = 0) const
Q3CStringupper() const
Q3CString &operator=(const Q3CString & s)
Q3CString &operator=(const QByteArray & ba)
Q3CString &operator=(const char * str)

Related Non-Members

Q_COMPAT_EXPORT_INLINEoperator!=(const Q3CString & s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINEoperator!=(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINEoperator!=(const char * s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINE constoperator+(const Q3CString & s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINE constoperator+(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINE constoperator+(const char * s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINE constoperator+(const Q3CString & s, char c)
Q_COMPAT_EXPORT_INLINE constoperator+(char c, const Q3CString & s)
Q_COMPAT_EXPORT_INLINEoperator<(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINEoperator<(const char * s1, const Q3CString & s2)
QDataStream &operator<<(QDataStream & s, const Q3CString & str)
Q_COMPAT_EXPORT_INLINEoperator<=(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINEoperator<=(const char * s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINEoperator==(const Q3CString & s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINEoperator==(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINEoperator==(const char * s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINEoperator>(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINEoperator>(const char * s1, const Q3CString & s2)
Q_COMPAT_EXPORT_INLINEoperator>=(const Q3CString & s1, const char * s2)
Q_COMPAT_EXPORT_INLINEoperator>=(const char * s1, const Q3CString & s2)
QDataStream &operator>>(QDataStream & s, Q3CString & str)

Additional Inherited Members

Detailed Description

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

Q3CString tries to behave like a more convenientconst 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 useQ3CString for text that is never exposed to the user, for most purposes, and especially for user-visible text, you should useQString.QString provides implicit sharing, Unicode and other internationalization support, and is well optimized.

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

A default constructedQ3CString isnull, i.e. both the length and the data pointer are 0 andisNull() returns true.

Note:However, if you ask for the data pointer of a nullQ3CString by callingdata(), then because the internal representation of the nullQ3CString 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 toisNull() will return false. But if you ask for the data pointer of a nullQ3CString by callingconstData(), the shared internal representation is not detached, a null data pointer is returned, and subsequent calls toisNull() will continue to return true.

AQ3CString that references the empty string ("", a single '\0' char) isempty, i.e.isEmpty() returns true. Both null and empty Q3CStrings are legal parameters to the methods. Assigningconst char * 0 toQ3CString produces a nullQ3CString.

Thelength() function returns the length of the string;resize() resizes the string andtruncate() truncates the string. A string can be filled with a character usingfill(). Strings can be left or right padded with characters usingleftJustify() andrightJustify(). Characters, strings and regular expressions can be searched for usingfind() andfindRev(), and counted usingcontains().

Strings and characters can be inserted with insert() and appended withappend(). 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 usingstripWhiteSpace() andsimplifyWhiteSpace(). Strings can be converted to uppercase or lowercase withupper() andlower() respectively.

Strings that contain numbers can be converted to numbers withtoShort(),toInt(),toLong(),toULong(),toFloat() andtoDouble(). Numbers can be converted to strings withsetNum().

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

Note on Character Comparisons

InQ3CString 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 includecontains(),find(),findRev(),operator<(),operator<=(),operator>(),operator>=(),lower() andupper().

This issue does not apply toQStrings since they represent characters using Unicode.

Performance note: TheQ3CString methods forQRegExp searching are implemented by converting theQ3CString to aQString and performing the search on that. This implies a deep copy of theQ3CString data. If you are going to perform manyQRegExp searches on a largeQ3CString, you will get better performance by converting theQ3CString to aQString yourself, and then searching in theQString.

Member Function Documentation

Q3CString::Q3CString()

Constructs a null string.

See alsoisNull().

Q3CString::Q3CString(int size)

Constructs a string with room forsize characters, including the '\0'-terminator. Makes a null string ifsize == 0.

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

See alsoresize() andisNull().

Q3CString::Q3CString(constQ3CString & s)

Constructs a shallow copys.

Q3CString::Q3CString(constQByteArray & ba)

Constructs a copy ofba.

Q3CString::Q3CString(constchar * str)

Constructs a string that is a deep copy ofstr.

Ifstr is 0 a null string is created.

See alsoisNull().

Q3CString::Q3CString(constchar * str,uint maxsize)

Constructs a string that is a deep copy ofstr. The copy will be at mostmaxsize bytes long including the '\0'-terminator.

Example:

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

Ifstr contains a 0 byte within the firstmaxsize bytes, the resultingQ3CString will be terminated by this 0. Ifstr is 0 a null string is created.

See alsoisNull().

Q3CString & Q3CString::append(constchar * str)

Appends stringstr 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 lengthwidth (plus one for the terminating '\0') that contains this string padded with thefill character.

If the length of the string exceedswidth andtruncate is false (the default), then the returned string is a copy of the string. If the length of the string exceedswidth andtruncate is true, then the returned string is a left(width).

Example:

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

See alsorightJustify().

Q3CString Q3CString::lower() const

UseQByteArray::toLower() instead.

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

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

If the length of the string exceedswidth andtruncate is false (the default), then the returned string is a copy of the string. If the length of the string exceedswidth andtruncate is true, then the returned string is a left(width).

Example:

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

See alsoleftJustify().

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

Sets the character at positionindex toc and expands the string if necessary, padding with spaces.

Returns false ifindex 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 numbern and returns a reference to the string.

The format of the string representation is specified by the format characterf, and the precision (number of digits after the decimal point) is specified withprec.

The valid formats forf are 'e', 'E', 'f', 'g' and 'G'. The formats are the same as forsprintf(); they are explained inQString::arg().

Q3CString & Q3CString::setNum(long n)

This is an overloaded function.

Sets the string to the string representation of the numbern and returns a reference to the string.

Q3CString & Q3CString::setNum(ulong n)

This is an overloaded function.

Sets the string to the string representation of the numbern and returns a reference to the string.

Q3CString & Q3CString::setNum(int n)

This is an overloaded function.

Sets the string to the string representation of the numbern and returns a reference to the string.

Q3CString & Q3CString::setNum(uint n)

This is an overloaded function.

Sets the string to the string representation of the numbern and returns a reference to the string.

Q3CString & Q3CString::setNum(short n)

This is an overloaded function.

Sets the string to the string representation of the numbern and returns a reference to the string.

Q3CString & Q3CString::setNum(ushort n)

This is an overloaded function.

Sets the string to the string representation of the numbern and returns a reference to the string.

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

This is an overloaded function.

Q3CString & Q3CString::setStr(constchar * str)

Makes a deep copy ofstr. Returns a reference to the string.

Q3CString Q3CString::simplifyWhiteSpace() const

UseQByteArray::simplified() instead.

Q3CString & Q3CString::sprintf(constchar * 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.

Example:

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

Warning: All vsprintf() implementations will write past the end of the target string (*this) if theformat 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

UseQByteArray::trimmed() instead.

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

Returns the string converted to adouble value.

Ifok 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 afloat value.

Ifok 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 aint value.

Ifok 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 along value.

Ifok 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 ashort value.

Ifok 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 anunsigned int value.

Ifok 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 anunsigned long value.

Ifok 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 anunsigned short value.

Ifok 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

UseQByteArray::toUpper() instead.

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

Assigns a shallow copy ofs to this string and returns a reference to this string.

Q3CString & Q3CString::operator=(constQByteArray & ba)

Assigns byte arrayba to thisQ3CString.

Q3CString & Q3CString::operator=(constchar * str)

This is an overloaded function.

Assigns a deep copy ofstr to this string and returns a reference to this string.

Ifstr is 0 a null string is created.

See alsoisNull().

Related Non-Members

Q_COMPAT_EXPORT_INLINEoperator!=(constQ3CString & s1, constQ3CString & s2)

Returns true ifs1 ands2 are different; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator!=(constQ3CString & s1, constchar * s2)

This is an overloaded function.

Returns true ifs1 ands2 are different; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator!=(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns true ifs1 ands2 are different; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINE constoperator+(constQ3CString & s1, constQ3CString & s2)

Returns a string which consists of the concatenation ofs1 ands2.

Q_COMPAT_EXPORT_INLINE constoperator+(constQ3CString & s1, constchar * s2)

This is an overloaded function.

Returns a string which consists of the concatenation ofs1 ands2.

Q_COMPAT_EXPORT_INLINE constoperator+(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns a string which consists of the concatenation ofs1 ands2.

Q_COMPAT_EXPORT_INLINE constoperator+(constQ3CString & s,char c)

This is an overloaded function.

Returns a string which consists of the concatenation ofs andc.

Q_COMPAT_EXPORT_INLINE constoperator+(char c, constQ3CString & s)

This is an overloaded function.

Returns a string which consists of the concatenation ofc ands.

Q_COMPAT_EXPORT_INLINEoperator<(constQ3CString & s1, constchar * s2)

Returns true ifs1 is less thans2; otherwise returns false.

Equivalent to qstrcmp(s1,s2) < 0.

Q_COMPAT_EXPORT_INLINEoperator<(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns true ifs1 is less thans2; otherwise returns false.

Equivalent to qstrcmp(s1,s2) < 0.

QDataStream &operator<<(QDataStream & s, constQ3CString & str)

Writes stringstr to the streams.

See alsoFormat of the QDataStream operators.

Q_COMPAT_EXPORT_INLINEoperator<=(constQ3CString & s1, constchar * s2)

Returns true ifs1 is less than or equal tos2; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator<=(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns true ifs1 is less than or equal tos2; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator==(constQ3CString & s1, constQ3CString & s2)

Returns true ifs1 ands2 are equal; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator==(constQ3CString & s1, constchar * s2)

This is an overloaded function.

Returns true ifs1 ands2 are equal; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator==(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns true ifs1 ands2 are equal; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator>(constQ3CString & s1, constchar * s2)

Returns true ifs1 is greater thans2; otherwise returns false.

Equivalent to qstrcmp(s1,s2) > 0.

Q_COMPAT_EXPORT_INLINEoperator>(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns true ifs1 is greater thans2; otherwise returns false.

Equivalent to qstrcmp(s1,s2) > 0.

Q_COMPAT_EXPORT_INLINEoperator>=(constQ3CString & s1, constchar * s2)

Returns true ifs1 is greater than or equal tos2; otherwise returns false.

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

Q_COMPAT_EXPORT_INLINEoperator>=(constchar * s1, constQ3CString & s2)

This is an overloaded function.

Returns true ifs1 is greater than or equal tos2; otherwise returns false.

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

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

Reads a string intostr from the streams.

See alsoFormat of the QDataStream operators.

© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.


[8]ページ先頭

©2009-2025 Movatter.jp