📄 qstring.3qt
字号:
.PPSee also section()..SH MEMBER FUNCTION DOCUMENTATION.SH "QString::QString ()"Constructs a null string, i.e. both the length and data pointer are 0..PPSee also isNull()..SH "QString::QString ( QChar ch )"Constructs a string of length one, containing the character \fIch\fR..SH "QString::QString ( const QString & s )"Constructs an implicitly shared copy of \fIs\fR. This is very fast since it only involves incrementing a reference count..SH "QString::QString ( const QByteArray & ba )"Constructs a string that is a deep copy of \fIba\fR interpreted as a classic C string..SH "QString::QString ( const QChar * unicode, uint length )"Constructs a string that is a deep copy of the first \fIlength\fR characters in the QChar array..PPIf \fIunicode\fR and \fIlength\fR are 0, then a null string is created..PPIf only \fIunicode\fR is 0, the string is empty but has \fIlength\fR characters of space preallocated: QString expands automatically anyway, but this may speed up some cases a little. We recommend using the plain constructor and setLength() for this purpose since it will result in more readable code..PPSee also isNull() and setLength()..SH "QString::QString ( const char * str )"Constructs a string that is a deep copy of \fIstr\fR, interpreted as a classic C string. The encoding is assumed to be Latin-1, unless you change it using QTextCodec::setCodecForCStrings()..PPIf \fIstr\fR is 0, then a null string is created..PPThis is a cast constructor, but it is perfectly safe: converting a Latin-1 \fCconst char *\fR to QString preserves all the information. You can disable this constructor by defining \fCQT_NO_CAST_ASCII\fR when you compile your applications. You can also make QString objects by using setLatin1(), fromLatin1(), fromLocal8Bit(), and fromUtf8(). Or whatever encoding is appropriate for the 8-bit data you have..PPSee also isNull() and fromAscii()..SH "QString::QString ( const std::string & str )"Constructs a string that is a deep copy of \fIstr\fR..PPThis is the same as fromAscii(\fIstr\fR)..SH "QString::~QString ()"Destroys the string and frees the string's data if this is the last reference to the string..SH "QString & QString::append ( const QString & str )"Appends \fIstr\fR to the string and returns a reference to the result..PP.nf.br string = "Test";.br string.append( "ing" ); // string == "Testing".br.fi.PPEquivalent to operator+=()..PPExample: dirview/dirview.cpp..SH "QString & QString::append ( char ch )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAppends character \fIch\fR to the string and returns a reference to the result..PPEquivalent to operator+=()..SH "QString & QString::append ( QChar ch )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAppends character \fIch\fR to the string and returns a reference to the result..PPEquivalent to operator+=()..SH "QString & QString::append ( const QByteArray & str )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAppends \fIstr\fR to the string and returns a reference to the result..PPEquivalent to operator+=()..SH "QString & QString::append ( const char * str )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAppends \fIstr\fR to the string and returns a reference to the result..PPEquivalent to operator+=()..SH "QString & QString::append ( const std::string & str )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPAppends \fIstr\fR to the string and returns a reference to the result..PPEquivalent to operator+=()..SH "QString QString::arg ( const QString & a, int fieldWidth = 0 ) const"This function will return a string that replaces the lowest numbered occurrence of \fC%1\fR, \fC%2\fR, ..., \fC%9\fR with \fIa\fR..PPThe \fIfieldWidth\fR value specifies the minimum amount of space that \fIa\fR is padded to. A positive value will produce right-aligned text, whereas a negative value will produce left-aligned text..PPThe following example shows how we could create a 'status' string when processing a list of files:.PP.nf.br QString status = QString( "Processing file %1 of %2: %3" ).br .arg( i ) // current file's number.br .arg( total ) // number of files to process.br .arg( fileName ); // current file's name.br.fi.PPIt 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..PPIf there is no place marker (\fC%1\fR, \fC%2\fR, etc.), a warning message (qWarning()) is output and the result is undefined..PP\fBWarning:\fR If any placeholder occurs more than once, the result is undefined..SH "QString QString::arg ( long a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThe \fIfieldWidth\fR value specifies the minimum amount of space that \fIa\fR is padded to. A positive value will produce a right-aligned number, whereas a negative value will produce a left-aligned number..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36..PPThe '%' can be followed by an 'L', in which case the sequence is replaced with a localized representation of \fIa\fR. The conversion uses the default locale. The default locale is determined from the system's locale settings at application startup. It can be changed using QLocale::setDefault(). The 'L' flag is ignored if \fIbase\fR is not 10..PP.nf.br QString str;.br str = QString( "Decimal 63 is %1 in hexadecimal" ).br .arg( 63, 0, 16 );.br // str == "Decimal 63 is 3f in hexadecimal".br.br QLocale::setDefault(QLocale::English, QLocale::UnitedStates);.br str = QString( "%1 %L2 %L3" ).br .arg( 12345 ).br .arg( 12345 ).br .arg( 12345, 0, 16 );.br // str == "12345 12,345 3039".br.fi.SH "QString QString::arg ( ulong a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::arg ( Q_LLONG a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::arg ( Q_ULLONG a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::arg ( int a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::arg ( uint a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::arg ( short a, int fieldWidth = 0, int base = 10 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::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..PP\fIa\fR is expressed in base \fIbase\fR, which is 10 by default and must be between 2 and 36. If \fIbase\fR is 10, the '%L' syntax can be used to produce localized strings..SH "QString QString::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..PPArgument \fIa\fR is formatted according to the \fIfmt\fR format specified, which is 'g' by default and can be any of the following:.PP<center>.nf.TSl - l. Format Meaning format as [-]9.9e[+|-]999 format as [-]9.9E[+|-]999 format as [-]9.9 use use.TE.fi</center>.PPWith 'e', 'E', and 'f', \fIprec\fR is the number of digits after the decimal point. With 'g' and 'G', \fIprec\fR is the maximum number of significant digits (trailing zeroes are omitted)..PP.nf.br double d = 12.34;.br QString ds = QString( "'E' format, precision 3, gives %1" ).br .arg( d, 0, 'E', 3 );.br // ds == "'E' format, precision 3, gives 1.234E+01".br.fi.PPThe '%L' syntax can be used to produce localized strings..SH "QString QString::arg ( char a, int fieldWidth = 0 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PP\fIa\fR is assumed to be in the Latin-1 character set..SH "QString QString::arg ( QChar a, int fieldWidth = 0 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..SH "QString QString::arg ( const QString & a1, const QString & a2 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThis is the same as str.arg(\fIa1\fR).arg(\fIa2\fR), except that the strings are replaced in one pass. This can make a difference if \fIa1\fR contains e.g. \fC%1\fR:.PP.nf.br QString str( "%1 %2" );.br str.arg( "Hello", "world" ); // returns "Hello world".br str.arg( "Hello" ).arg( "world" ); // returns "Hello world".br.br str.arg( "(%1)", "Hello" ); // returns "(%1) Hello".br str.arg( "(%1)" ).arg( "Hello" ); // returns "(Hello) %2".br.fi.SH "QString QString::arg ( const QString & a1, const QString & a2, const QString & a3 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThis is the same as calling str.arg(\fIa1\fR).arg(\fIa2\fR).arg(\fIa3\fR), except that the strings are replaced in one pass..SH "QString QString::arg ( const QString & a1, const QString & a2, const QString & a3, const QString & a4 ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThis is the same as calling str.arg(\fIa1\fR).arg(\fIa2\fR).arg(\fIa3\fR).arg(\fIa4\fR), except that the strings are replaced in one pass..SH "const char * QString::ascii () const"Returns an 8-bit ASCII representation of the string..PPIf a codec has been set using QTextCodec::codecForCStrings(), it is used to convert Unicode to 8-bit char. Otherwise, this function does the same as latin1()..PPSee also fromAscii(), latin1(), utf8(), and local8Bit()..PPExample: network/networkprotocol/nntp.cpp..SH "QChar QString::at ( uint i ) const"Returns the character at index \fIi\fR, or 0 if \fIi\fR is beyond the length of the string..PP.nf.br const QString string( "abcdefgh" );.br QChar ch = string.at( 4 );.br // ch == 'e'.br.fi.PPIf the QString is not const (i.e. const QString) or const& (i.e. const QString &), then the non-const overload of at() will be used instead..SH "QCharRef QString::at ( uint i )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPThe function returns a reference to the character at index \fIi\fR. 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..PPIf \fIi\fR is beyond the length of the string then the string is expanded with QChar::null..SH "uint QString::capacity () const"Returns the number of characters this string can hold in the allocated memory..PPSee also reserve() and squeeze()..SH "int QString::compare ( const QString & s1, const QString & s2 )\fC [static]\fR"Lexically compares \fIs1\fR with \fIs2\fR and returns an integer less than, equal to, or greater than zero if \fIs1\fR is less than, equal to, or greater than \fIs2\fR..PPThe 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 QString::localeAwareCompare()..PP.nf.br int a = QString::compare( "def", "abc" ); // a > 0.br int b = QString::compare( "abc", "def" ); // b < 0.br int c = QString::compare( "abc", "abc" ); // c == 0.br.fi.SH "int QString::compare ( const QString & s ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPLexically compares this string with \fIs\fR and returns an integer less than, equal to, or greater than zero if it is less than, equal to, or greater than \fIs\fR..SH "void QString::compose ()"\fBWarning:\fR This function is not supported in Qt 3.x. It is provided for experimental and illustrative purposes only. It is mainly of interest to those experimenting with Arabic and other composition-rich texts..PPApplies possible ligatures to a QString. Useful when composition-rich text requires rendering with glyph-poor fonts, but it also makes compositions such as QChar(0x0041) ('A') and QChar(0x0308) (Unicode accent diaresis), giving QChar(0x00c4) (German A Umlaut)..SH "QChar QString::constref ( uint i ) const"Returns the QChar at index \fIi\fR by value..PPEquivalent to at(\fIi\fR)..PPSee also ref()..SH "int QString::contains ( QChar c, bool cs = TRUE ) const"Returns the number of times the character \fIc\fR occurs in the string..PPIf \fIcs\fR is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive..PP.nf.br QString string( "Trolltech and Qt" );.br int n = string.contains( 't', FALSE );.br // n == 3.br.fi.PPExamples:.)l fileiconview/qfileiconview.cpp and mdi/application.cpp..SH "int QString::contains ( char c, bool cs = TRUE ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..SH "int QString::contains ( const char * str, bool cs = TRUE ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns the number of times the string \fIstr\fR occurs in the string..PPIf \fIcs\fR is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive..SH "int QString::contains ( const QString & str, bool cs = TRUE ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns the number of times \fIstr\fR occurs in the string..PPIf \fIcs\fR is TRUE (the default), the search is case sensitive; otherwise the search is case insensitive..PPThis function counts overlapping strings, so in the example below, there are two instances of "ana" in "bananas"..PP.nf.br QString str( "bananas" );.br int i = str.contains( "ana" ); // i == 2.br.fi.PPSee also findRev()..SH "int QString::contains ( const QRegExp & rx ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPReturns the number of times the regexp, \fIrx\fR, matches in the string..PPThis function counts overlapping matches, so in the example below, there are four instances of "ana" or "ama"..PP.nf.br
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -