📄 qstring.3qt
字号:
.br QString string( "bananas" );.br int i = string.findRev( QRegExp("an") ); // i == 3.br.fi.PPSee also find()..SH "QString QString::fromAscii ( const char * ascii, int len = -1 )\fC [static]\fR"Returns the Unicode string decoded from the first \fIlen\fR characters of \fIascii\fR, ignoring the rest of \fIascii\fR. If \fIlen\fR is -1 then the length of \fIascii\fR is used. If \fIlen\fR is bigger than the length of \fIascii\fR then it will use the length of \fIascii\fR..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 fromLatin1()..PPThis is the same as the QString(const char*) constructor, but you can make that constructor invisible if you compile with the define \fCQT_NO_CAST_ASCII\fR, in which case you can explicitly create a QString from 8-bit ASCII text using this function..PP.nf.br QString str = QString::fromAscii( "123456789", 5 );.br // str == "12345".br.fi.SH "QString QString::fromLatin1 ( const char * chars, int len = -1 )\fC [static]\fR"Returns the Unicode string decoded from the first \fIlen\fR characters of \fIchars\fR, ignoring the rest of \fIchars\fR. If \fIlen\fR is -1 then the length of \fIchars\fR is used. If \fIlen\fR is bigger than the length of \fIchars\fR then it will use the length of \fIchars\fR..PPSee also fromAscii()..PPExamples:.)l listbox/listbox.cpp and network/mail/smtp.cpp..SH "QString QString::fromLocal8Bit ( const char * local8Bit, int len = -1 )\fC [static]\fR"Returns the Unicode string decoded from the first \fIlen\fR characters of \fIlocal8Bit\fR, ignoring the rest of \fIlocal8Bit\fR. If \fIlen\fR is -1 then the length of \fIlocal8Bit\fR is used. If \fIlen\fR is bigger than the length of \fIlocal8Bit\fR then it will use the length of \fIlocal8Bit\fR..PP.nf.br QString str = QString::fromLocal8Bit( "123456789", 5 );.br // str == "12345".br.fi.PP\fIlocal8Bit\fR is assumed to be encoded in a locale-specific format..PPSee QTextCodec for more diverse coding/decoding of Unicode strings..SH "QString QString::fromUcs2 ( const unsigned short * str )\fC [static]\fR"Constructs a string that is a deep copy of \fIstr\fR, interpreted as a UCS2 encoded, zero terminated, Unicode string..PPIf \fIstr\fR is 0, then a null string is created..PPSee also isNull()..SH "QString QString::fromUtf8 ( const char * utf8, int len = -1 )\fC [static]\fR"Returns the Unicode string decoded from the first \fIlen\fR characters of \fIutf8\fR, ignoring the rest of \fIutf8\fR. If \fIlen\fR is -1 then the length of \fIutf8\fR is used. If \fIlen\fR is bigger than the length of \fIutf8\fR then it will use the length of \fIutf8\fR..PP.nf.br QString str = QString::fromUtf8( "123456789", 5 );.br // str == "12345".br.fi.PPSee QTextCodec for more diverse coding/decoding of Unicode strings..PPExample: fonts/simple-qfont-demo/viewer.cpp..SH "QString & QString::insert ( uint index, const QString & s )"Inserts \fIs\fR into the string at position \fIindex\fR..PPIf \fIindex\fR is beyond the end of the string, the string is extended with spaces to length \fIindex\fR and \fIs\fR is then appended and returns a reference to the string..PP.nf.br QString string( "I like fish" );.br str = string.insert( 2, "don't " );.br // str == "I don't like fish".br.fi.PPSee also remove() and replace()..PPExamples:.)l themes/themes.cpp and xform/xform.cpp..SH "QString & QString::insert ( uint index, const QChar * s, uint len )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPInserts the character in \fIs\fR into the string at position \fIindex\fR \fIlen\fR number of times and returns a reference to the string..SH "QString & QString::insert ( uint index, QChar c )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPInsert \fIc\fR into the string at position \fIindex\fR and returns a reference to the string..PPIf \fIindex\fR is beyond the end of the string, the string is extended with spaces (ASCII 32) to length \fIindex\fR and \fIc\fR is then appended..SH "QString & QString::insert ( uint index, char c )"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPInsert character \fIc\fR at position \fIindex\fR..SH "bool QString::isEmpty () const"Returns TRUE if the string is empty, i.e. if length() == 0; otherwise returns FALSE. Null strings are also empty..PP.nf.br QString a( "" );.br a.isEmpty(); // TRUE.br a.isNull(); // FALSE.br.br QString b;.br b.isEmpty(); // TRUE.br b.isNull(); // TRUE.br.fi.PPSee also isNull() and length()..PPExamples:.)l addressbook/mainwindow.cpp, chart/chartform.cpp, chart/chartform_canvas.cpp, network/networkprotocol/nntp.cpp, qmag/qmag.cpp, and qwerty/qwerty.cpp..SH "bool QString::isNull () const"Returns TRUE if the string is null; otherwise returns FALSE. A null string is always empty..PP.nf.br QString a; // a.unicode() == 0, a.length() == 0.br a.isNull(); // TRUE, because a.unicode() == 0.br a.isEmpty(); // TRUE.br.fi.PPSee also isEmpty() and length()..PPExamples:.)l i18n/main.cpp, network/ftpclient/ftpmainwindow.ui.h, and qdir/qdir.cpp..SH "const char * QString::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 QTextCodec classes..PPThis function is mainly useful for boot-strapping legacy code to use Unicode..PPThe result remains valid so long as one unmodified copy of the source string exists..PPSee also fromLatin1(), ascii(), utf8(), and local8Bit()..PPExamples:.)l fileiconview/qfileiconview.cpp and network/networkprotocol/nntp.cpp..SH "QString QString::left ( uint len ) const"Returns a substring that contains the \fIlen\fR leftmost characters of the string..PPThe whole string is returned if \fIlen\fR exceeds the length of the string..PP.nf.br QString s = "Pineapple";.br QString t = s.left( 4 ); // t == "Pine".br.fi.PPSee also right(), mid(), and isEmpty()..PPExample: themes/themes.cpp..SH "QString QString::leftJustify ( uint width, QChar fill = ' ', bool truncate = FALSE ) const"Returns a string of length \fIwidth\fR that contains this string padded by the \fIfill\fR character..PPIf \fItruncate\fR is FALSE and the length of the string is more than \fIwidth\fR, then the returned string is a copy of the string..PPIf \fItruncate\fR is TRUE and the length of the string is more than \fIwidth\fR, then any characters in a copy of the string after length \fIwidth\fR are removed, and the copy is returned..PP.nf.br QString s( "apple" );.br QString t = s.leftJustify( 8, '.' ); // t == "apple...".br.fi.PPSee also rightJustify()..SH "uint QString::length () const"Returns the length of the string..PPNull strings and empty strings have zero length..PPSee also isNull() and isEmpty()..PPExamples:.)l fileiconview/qfileiconview.cpp, network/networkprotocol/nntp.cpp, rot13/rot13.cpp, and themes/themes.cpp..SH "QCString QString::local8Bit () const"Returns the string encoded in a locale-specific format. On X11, this is the QTextCodec::codecForLocale(). On Windows, it is a system-defined encoding. On Mac OS X, this always uses UTF-8 as the encoding..PPSee QTextCodec for more diverse coding/decoding of Unicode strings..PPSee also fromLocal8Bit(), ascii(), latin1(), and utf8()..SH "int QString::localeAwareCompare ( const QString & s1, const QString & s2 )\fC [static]\fR"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 performed in a locale- and also platform-dependent manner. Use this function to present sorted lists of strings to the user..PPSee also QString::compare() and QTextCodec::locale()..SH "int QString::localeAwareCompare ( const QString & s ) const"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPCompares this string with \fIs\fR..SH "QString QString::lower () const"Returns a lowercase copy of the string..PP.nf.br QString string( "TROlltECH" );.br str = string.lower(); // str == "trolltech".br.fi.PPSee also upper()..PPExample: scribble/scribble.cpp..SH "QString QString::mid ( uint index, uint len = 0xffffffff ) const"Returns a string that contains the \fIlen\fR characters of this string, starting at position \fIindex\fR..PPReturns a null string if the string is empty or \fIindex\fR is out of range. Returns the whole string from \fIindex\fR if \fIindex\fR + \fIlen\fR exceeds the length of the string..PP.nf.br QString s( "Five pineapples" );.br QString t = s.mid( 5, 4 ); // t == "pine".br.fi.PPSee also left() and right()..PPExamples:.)l network/mail/smtp.cpp, qmag/qmag.cpp, and themes/themes.cpp..SH "QString QString::number ( long n, int base = 10 )\fC [static]\fR"A convenience function that returns a string equivalent of the number \fIn\fR to base \fIbase\fR, which is 10 by default and must be between 2 and 36..PP.nf.br long a = 63;.br QString str = QString::number( a, 16 ); // str == "3f".br QString str = QString::number( a, 16 ).upper(); // str == "3F".br.fi.PPSee also setNum()..PPExamples:.)l action/application.cpp, application/application.cpp, chart/chartform.cpp, fonts/simple-qfont-demo/viewer.cpp, helpviewer/helpwindow.cpp, mdi/application.cpp, and sql/overview/extract/main.cpp..SH "QString QString::number ( ulong n, int base = 10 )\fC [static]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPSee also setNum()..SH "QString QString::number ( int n, int base = 10 )\fC [static]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPSee also setNum()..SH "QString QString::number ( uint n, int base = 10 )\fC [static]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPA convenience factory function that returns a string representation of the number \fIn\fR to the base \fIbase\fR, which is 10 by default and must be between 2 and 36..PPSee also setNum()..SH "QString QString::number ( double n, char f = 'g', int prec = 6 )\fC [static]\fR"This is an overloaded member function, provided for convenience. It behaves essentially like the above function..PPArgument \fIn\fR is formatted according to the \fIf\fR format specified, which is \fCg\fR 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 == "1.234E+001".br.fi.PPSee also setNum()..SH "QString::operator const char * () const"Returns latin1(). Be sure to see the warnings documented in the latin1() function. Note that for new code which you wish to be strictly Unicode-clean, you can define the macro \fCQT_NO_ASCII_CAST\fR 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!()..SH "QString::operator std::string () const"Returns ascii()..SH "bool QString::operator! () const"Returns TRUE if this is a null string; otherwise returns FALSE..PP.nf.br QString name = getName();.br if ( !name ).br name = "Rodney";.br.fi.PPNote that if you say.PP.nf.br QString name = getName();.br if ( name ).br doSomethingWith(name);.br.fi.PPIt will call "operator const char*()", which is inefficent; you may wish to define the macro \fCQT_NO_ASCII_CAST\fR when writing code which you wish to remain Unicode-clean..PPWhen you want the above semantics, use:.PP.nf.br QString name = getName();.br if ( !name.isNull() ).br doSomethingWith(name);.br.fi.PPSee also isEmpty()..SH "QString & QString::operator+= ( const QString & str )"Appends \fIstr\fR to the string and returns a reference to the string..SH "QString & QString::operator+= ( 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 string.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -