1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qstringlist.h"
31
#include "qunicodetables_p.h"
32
#ifndef QT_NO_TEXTCODEC
33
#include <qtextcodec.h>
35
#include <qdatastream.h>
38
#include "qlocale_p.h"
39
#include "qstringmatcher.h"
51
# if defined (Q_CC_GNU) && (__GNUC__ - 0 >= 3)
61
#define LLONG_MAX qint64_C(9223372036854775807)
64
#define LLONG_MIN (-LLONG_MAX - qint64_C(1))
67
#define ULLONG_MAX quint64_C(18446744073709551615)
71
#ifndef QT_NO_TEXTCODEC
72
QTextCodec *QString::codecForCStrings;
76
static QHash<void *, QByteArray> *asciiCache = 0;
79
static int ucstrcmp(const QString &as, const QString &bs)
81
const QChar *a = as.unicode();
82
const QChar *b = bs.unicode();
89
int l=qMin(as.length(),bs.length());
90
while (l-- && *a == *b)
93
return (as.length()-bs.length());
94
return a->unicode() - b->unicode();
97
static int ucstrncmp(const QChar *a, const QChar *b, int l)
99
while (l-- && *a == *b)
103
return a->unicode() - b->unicode();
106
static int ucstrnicmp(const QChar *a, const QChar *b, int l)
108
while (l-- && ::lower(*a) == ::lower(*b))
112
return ::lower(*a).unicode() - ::lower(*b).unicode();
116
inline bool qIsUpper(char ch)
118
return ch >= 'A' && ch <= 'Z';
121
inline bool qIsDigit(char ch)
123
return ch >= '0' && ch <= '9';
126
inline char qToLower(char ch)
128
if (ch >= 'A' && ch <= 'Z')
129
return ch - 'A' + 'a';
134
const QString::Null QString::null = QString::Null();
139
\brief The QCharRef class is a helper class for QString.
145
When you get an object of type QCharRef, if you can assign to it,
146
the assignment will apply to the character in the string from
147
which you got the reference. That is its whole purpose in life.
148
The QCharRef becomes invalid once modifications are made to the
149
string: if you want to keep the character, copy it into a QChar.
151
Most of the QChar member functions also exist in QCharRef.
152
However, they are not explicitly documented here.
154
\sa QString::operator[]() QString::at() QChar
161
\brief The QString class provides a Unicode character string.
169
QString stores a string of 16-bit \l{QChar}s, where each QChar
170
stores one Unicode 4.0 character. \l{Unicode} is an international
171
standard that supports most of the writing systems in use today.
172
It is a superset of ASCII and Latin-1 (ISO 8859-1), and all the
173
ASCII/Latin-1 characters are available at the same code
176
Behind the scenes, QString uses \l{implicit sharing}
177
(copy-on-write) to reduce memory usage and to avoid the needless
178
copying of data. This also helps reduce the inherent overhead of
179
storing 16-bit characters instead of 8-bit characters.
181
In addition to QString, Qt also provides the QByteArray class to
182
store raw bytes and traditional 8-bit '\\0'-terminated strings.
183
For most purposes, QString is the class you want to use. It is
184
used throughout the Qt API, and the Unicode support ensures that
185
your applications will be easy to translate if you want to expand
186
your application's market at some point. The two main cases where
187
QByteArray is appropriate are when you need to store raw binary
188
data, and when memory conservation is critical (e.g. with
191
One way to initialize a QString is simply to pass a \c{const char
192
*} to its constructor. For example, the following code creates a
193
QString of size 5 containing the data "Hello":
196
QString str = "Hello";
199
QString converts the \c{const char *} data into Unicode using
200
fromAscii(). By default, fromAscii() treats character above 128
201
as Latin-1 characters, but this can be changed by calling
202
QTextCodec::setCodecForCStrings().
204
In all of the QString methods that take \c{const char *}
205
parameters, the \c{const char *} is interpreted as a classic
206
C-style '\\0'-terminated string. It is legal for the \c{const
207
char *} parameter to be 0.
209
You can also provide string data as an array of \l{QChar}s:
212
static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
213
QString str(data, 4);
216
QString makes a deep copy of the QChar data, so you can modify it
217
later without experiencing side effects. (If for performance
218
reasons you don't want to take a deep copy of the character data,
219
use QString::fromRawData() instead.)
221
Another approach is to set the size of the string using resize()
222
and to initialize the data character per character. QString uses
223
0-based indexes, just like C++ arrays. To access the character at
224
a particular index position, you can use operator[](). On
225
non-const strings, operator[]() returns a reference to a
226
character that can be used on the left side of an assignment. For
234
str[2] = QChar(0x10e3);
235
str[3] = QChar(0x03a3);
238
For read-only access, an alternative syntax is to use at():
241
for (int i = 0; i < str.size(); ++i) {
242
if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
243
cout << "Found character in range [a-f]" << endl;
247
at() can be faster than operator[](), because it never causes a
248
\l{deep copy} to occur.
250
To extract several characters at a time, use left(), right(), or
253
A QString can embed '\\0' characters (QChar::null). The size()
254
function always returns the size of the whole string, including
255
embedded '\\0' characters.
257
After a call to resize(), newly allocated characters have
258
undefined values. To set all the characters in the string to a
259
particular value, call fill().
261
QString provides dozens of overloads designed to simplify string
262
usage. For example, if you want to compare a QString with a string
263
literal, you can write code like this and it will work as expected:
266
if (str == "auto" || str == "extern"
267
|| str == "static" || str == "register") {
272
You can also pass string literals to functions that take QStrings
273
and the QString(const char *) constructor will be invoked.
274
Similarly, you can pass a QString to a function that takes a
275
\c{const char *} and \l{operator const char *()} will be invoked.
277
QString provides the following basic functions for modifying the
278
character data: append(), prepend(), insert(), replace(), and
279
remove(). For example:
283
str.prepend("rock "); // str == "rock and"
284
str.append(" roll"); // str == "rock and roll"
285
str.replace(5, 3, "&"); // str == "rock & roll"
288
The replace() and remove() functions' first two arguments are the
289
position from which to start erasing and the number of characters
290
that should be erased.
292
A frequent requirement is to remove whitespace characters from a
293
string ('\\n', '\\t', ' ', etc.). If you want to remove whitespace
294
from both ends of a QString, use trimmed(). If you want to remove
295
whitespace from both ends and replace multiple consecutive
296
whitespaces with a single space character within the string, use
299
If you want to find all occurrences of a particular character or
300
substring in a QString, use indexOf() or lastIndexOf(). The
301
former searches forward starting from a given index position, the
302
latter searches backward. Both return the index position of the
303
character or substring if they find it; otherwise, they return -1.
304
For example, here's a typical loop that finds all occurrences of a
305
particular substring:
308
QString str = "We must be <b>bold</b>, very <b>bold</b>";
310
while ((j = str.indexOf("<b>", j)) != -1) {
311
cout << "Found <b> tag at index position " << j << endl;
316
If you want to see if a QString starts or ends with a particular
317
substring use startsWith() or endsWith(). If you simply want to
318
check whether a QString contains a particular character or
319
substring, use contains(). If you want to find out how many times
320
a particular character or substring occurs in the string, use
323
QString provides many functions for converting numbers into
324
strings and strings into numbers. See the arg() functions, the
325
setNum() functions, the number() static functions, and the
326
toInt(), toDouble(), and similar functions.
328
To get an upper or lower case version of a string use toUpper() or
331
If you want to replace all occurrences of a particular substring
332
with another, use one of the two-parameter replace() overloads.
334
QStrings can be compared using overloaded operators such as
335
operator<(), operator<=(), operator==(), operator>=(), and so on.
336
The comparison is based exclusively on the numeric Unicode values
337
of the characters and is very fast, but is not what a human would
338
expect. QString::localeAwareCompare() is a better choice for
339
sorting user-interface strings.
341
Lists of strings are handled by the QStringList class. You can
342
split a string into a list of strings using split(), and join a
343
list of strings into a single string with an optional separator
344
using QStringList::join(). You can obtain a list of strings from
345
a string list that contain a particular substring or that match a
346
particular QRegExp using QStringList::find().
348
If you are building a QString gradually and know in advance
349
approximately how many characters the QString will contain, you
350
can call reserve(), asking QString to preallocate a certain amount
351
of memory. You can also call capacity() to find out how much
352
memory QString actually allocated.
354
To obtain a pointer to the actual character data, call data() or
355
constData(). These functions return a pointer to the beginning of
356
the QChar data. The pointer is guaranteed to remain valid until a
357
non-const function is called on the QString.
359
\section1 Conversions between 8-bit strings and Unicode strings
361
QString provides the following four functions that return a
362
\c{const char *} version of the string as QByteArray: toAscii(),
363
toLatin1(), toUtf8(), and toLocal8Bit().
366
\i toAscii() returns an ASCII encoded 8-bit string.
367
\i toLatin1() returns a Latin-1 (ISO 8859-1) encoded 8-bit string.
368
\i toUtf8() returns a UTF-8 encoded 8-bit string. UTF-8 is a
369
superset of ASCII that supports the entire Unicode character
370
set through multibyte sequences.
371
\i toLocal8Bit() returns an 8-bit string using the system's local
375
To convert from one of these encodings, QString provides
376
fromAscii(), fromLatin1(), fromUtf8(), and fromLocal8Bit(). Other
377
encodings are supported through QTextCodec.
379
As mentioned above, QString provides a lot of functions and
380
operators that make it easy to interoperate with \c{const char *}
381
strings. This functionaly is a two-edged sword: It makes QString
382
more convenient to use if all strings are ASCII or Latin-1, but
383
there is always the risk that an implicit conversion from or to
384
\c{const char *} is done using the wrong 8-bit encoding. To
385
minimize these risks, you can turn off these implicit conversions
386
by defining these two preprocessor symbols:
389
\i \c QT_NO_CAST_FROM_ASCII disables automatic conversions from
391
\i \c QT_NO_CAST_TO_ASCII disables automatic conversion from QString
395
One way to define these prepocessor symbols globally for your
396
application is to add the following entry to your
397
\l{qmake Project Files}{qmake project file}:
400
DEFINES += QT_NO_CAST_FROM_ASCII \
404
You then need to explicitly call fromAscii(), fromLatin1(),
405
fromUtf8(), or fromLocal8Bit() to construct a QString from an
406
8-bit string, or use the lightweight QLatin1String class, for
410
QString url = QLatin1String("http://www.unicode.org/");
413
Similarly, you must call toAscii(), toLatin1(), toUtf8(), or
414
toLocal8Bit() explicitly to convert the QString to an 8-bit string.
415
(Other encodings are supported through QTextCodec.)
417
\section1 Note for C programmers
419
Due to C++'s type system and the fact that QString is
420
\l{implicitly shared}, QStrings may be treated like \c{int}s or
421
other basic types. For example:
424
QString boolToString(bool b)
435
The variable, result, is a normal variable allocated on the
436
stack. When return is called, because we're returning by value,
437
The copy constructor is called and a copy of the string is
438
returned. (No actual copying takes place thanks to the implicit
441
\section1 Distinction between null and empty strings
443
For historical reasons, QString distinguishes between a null
444
string and an empty string. A \e null string is a string that is
445
initialized using QString's default constructor or by passing
446
(const char *)0 to the constructor. An \e empty string is any
447
string with size 0. A null string is always empty, but an empty
448
string isn't necessarily null:
451
QString().isNull(); // returns true
452
QString().isEmpty(); // returns true
454
QString("").isNull(); // returns false
455
QString("").isEmpty(); // returns true
457
QString("abc").isNull(); // returns false
458
QString("abc").isEmpty(); // returns false
461
All functions except isNull() treat null strings the same as
462
empty strings. For example, toAscii().constData() returns a
463
pointer to a '\\0' character for a null string (\e not a null pointer),
464
and QString() compares equal to QString(""). We recommend that you
465
always use isEmpty() and avoid isNull().
467
\sa fromRawData(), QChar, QLatin1String, QByteArray
471
\enum QString::SplitBehavior
473
\value KeepEmptyParts
474
\value SkipEmptyParts
477
QString::Data QString::shared_null = { Q_ATOMIC_INIT(1), 0, 0, shared_null.array, 0, 0, 0, 0, 0, {0} };
478
QString::Data QString::shared_empty = { Q_ATOMIC_INIT(1), 0, 0, shared_empty.array, 0, 0, 0, 0, 0, {0} };
480
inline int QString::grow(int size)
482
return qAllocMore(size * sizeof(QChar), sizeof(Data)) / sizeof(QChar);
485
/*! \typedef QString::ConstIterator
489
Qt-style synonym for QString::const_iterator.
492
/*! \typedef QString::Iterator
496
Qt-style synonym for QString::iterator.
499
/*! \typedef QString::const_iterator
503
The QString::const_iterator typedef provides an STL-style const
504
iterator for QString.
506
\sa QString::iterator
509
/*! \typedef QString::iterator
513
The QString::iterator typedef provides an STL-style non-const
514
iterator for QString.
516
\sa QString::const_iterator
519
/*! \fn QString::iterator QString::begin()
524
/*! \fn QString::const_iterator QString::begin() const
529
/*! \fn QString::const_iterator QString::constBegin() const
534
/*! \fn QString::iterator QString::end()
539
/*! \fn QString::const_iterator QString::end() const
544
/*! \fn QString::const_iterator QString::constEnd() const
550
\fn QString::QString()
552
Constructs a null string. Null strings are also empty.
557
/*! \fn QString::QString(const char *str)
559
Constructs a string initialized with the ASCII string \a str. \a
560
str is converted to Unicode using fromAscii().
562
You can disable this constructor by defining \c
563
QT_NO_CAST_FROM_ASCII when you compile your applications. This
564
can be useful if you want to ensure that all user-visible strings
565
go through QObject::tr(), for example.
567
\sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
570
/*! \fn QString QString::fromStdString(const std::string &str)
572
Returns a copy of \a str. \a str is converted to Unicode using
575
This constructor is only available if Qt is configured with STL
576
compabitility enabled.
578
\sa fromAscii(), fromLatin1(), fromLocal8Bit(), fromUtf8()
582
/*! \fn QString QString::fromStdWString(const std::wstring &str)
584
Returns a copy of \a str. \a str is assumed to be encoded in
585
utf16 if the size of wchar_t is 2 bytes (e.g. on windows) and ucs4
586
if the size of wchar_t is 4 bytes (most Unix systems).
588
This constructor is only available if Qt is configured with STL
589
compabitility enabled.
591
\sa fromUtf16(), fromLatin1(), fromLocal8Bit(), fromUtf8()
596
QString QString::fromWCharArray(const wchar_t *a, int l)
599
if (sizeof(wchar_t) == sizeof(QChar)) {
600
s = fromUtf16((ushort *)a);
602
s.resize(l*2); // worst case
603
QChar *uc = s.data();
604
for (int i = 0; i < l; ++i) {
607
// decompose into a surrogate pair
609
*uc = QChar(u/0x400 + 0xd800);
611
u = u%0x400 + 0xdc00;
616
s.resize(uc - s.data());
621
/*! \fn std::wstring QString::toStdWString() const
623
Returns a std::wstring object with the data contained in this
624
QString. The std::wstring is encoded in utf16 on platforms where
625
wchar_t is 2 bytes wide (e.g. windows) and in ucs4 on platforms
626
where wchar_t is 4 bytes wide (most Unix systems).
628
This operator is mostly useful to pass a QString to a function
629
that accepts a std::wstring object.
631
This operator is only available if Qt is configured with STL
632
compabitility enabled.
634
\sa utf16(), toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
639
int QString::toWCharArray(wchar_t *array) const
641
if (sizeof(wchar_t) == sizeof(QChar)) {
642
memcpy(array, utf16(), sizeof(wchar_t)*length());
646
const unsigned short *uc = utf16();
647
for (int i = 0; i < length(); ++i) {
649
if (u >= 0xd800 && u < 0xdc00 && i < length()-1) {
650
ushort low = uc[i+1];
651
if (low >= 0xdc00 && low < 0xe000) {
653
u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
664
/*! \fn QString::QString(const QString &other)
666
Constructs a copy of \a other.
668
This operation takes \l{constant time}, because QString is
669
\l{implicitly shared}. This makes returning a QString from a
670
function very fast. If a shared instance is modified, it will be
671
copied (copy-on-write), and that takes \l{linear time}.
677
Constructs a string initialized with the first \a size characters
678
of the QChar array \a unicode.
680
QString makes a deep copy of the string data.
682
QString::QString(const QChar *unicode, int size)
687
} else if (size <= 0) {
691
d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
693
d->alloc = d->size = size;
694
d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
696
memcpy(d->array, unicode, size * sizeof(QChar));
697
d->array[size] = '\0';
703
Constructs a string of size \a size with every character set to
708
QString::QString(int size, QChar ch)
714
d = (Data*) qMalloc(sizeof(Data)+size*sizeof(QChar));
716
d->alloc = d->size = size;
717
d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
719
d->array[size] = '\0';
720
QChar *i = (QChar*)d->array + size;
721
QChar *b = (QChar*)d->array;
727
/*! \fn QString::QString(const QLatin1String &str)
729
Constructs a copy of the Latin-1 string \a str.
735
Constructs a string of size 1 containing the character \a ch.
737
QString::QString(QChar ch)
739
d = (Data *)qMalloc(sizeof(Data) + sizeof(QChar));
741
d->alloc = d->size = 1;
742
d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
744
d->array[0] = ch.unicode();
748
/*! \fn QString::QString(const QByteArray &ba)
750
Constructs a string initialized with the byte array \a ba. \a ba
751
is converted to Unicode using fromAscii().
753
You can disable this constructor by defining \c
754
QT_NO_CAST_FROM_ASCII when you compile your applications. This
755
can be useful if you want to ensure that all user-visible strings
756
go through QObject::tr(), for example.
759
/*! \fn QString::QString(const Null &)
763
/*! \fn QString &QString::operator=(const Null &)
768
\fn QString::~QString()
774
/*! \fn void QString::detach()
779
/*! \fn void QString::isDetached() const
784
void QString::free(Data *d)
788
Q_ASSERT(asciiCache);
789
asciiCache->remove(d);
796
Sets the size of the string to \a size characters.
798
If \a size is greater than the current size, the string is
799
extended to make it \a size characters long with the extra
800
characters added to the end. The new characters are uninitialized.
802
If \a size is less than the current size, characters are removed
807
QString str = "Hello world";
812
// str == "Hello???" (where ? stands for any character)
815
If you want to append a certain number of identical characters to
816
the string, use operator+=() as follows rather than resize():
819
QString str = "Hello";
820
str += QString(10, 'X');
821
// str == "HelloXXXXXXXXXX"
824
If you want to expand the string so that it reaches a certain
825
width and fill the new positions with a particular character, use
829
QString str = "Hello";
830
str = str.leftJustified(10, ' ');
834
\sa truncate(), reserve()
837
void QString::resize(int size)
840
Data *x = &shared_empty;
842
x = qAtomicSetPtr(&d, x);
846
if (d->ref != 1 || size > d->alloc || (size < d->size && size < d->alloc >> 1))
848
if (d->alloc >= size) {
851
d->array[size] = '\0';
856
/*! \fn int QString::capacity() const
858
Returns the maximum number of characters that can be stored in
859
the string without forcing a reallocation.
861
The sole purpose of this function is to provide a means of fine
862
tuning QString's memory usage. In general, you will rarely ever
863
need to call this function. If you want to know how many
864
characters are in the string, call size().
866
\sa reserve(), squeeze()
870
\fn void QString::reserve(int size)
872
Attempts to allocate memory for at least \a size characters. If
873
you know in advance how large the string will be, you can call
874
this function, and if you resize the string often you are likely
875
to get better performance. If \a size is an underestimate, the
876
worst that will happen is that the QString will be a bit slower.
878
The sole purpose of this function is to provide a means of fine
879
tuning QString's memory usage. In general, you will rarely ever
880
need to call this function. If you want to change the size of the
881
string, call resize().
883
This function is useful for code that needs to build up a long
884
string and wants to avoid repeated reallocation. In this example,
885
we want to add to the string until some condition is true, and
886
we're fairly sure that size is large enough to make a call to
887
reserve() worthwhile:
892
result.reserve(maxSize);
894
result[len++] = getNextChar(); // fill part of the space
899
\sa squeeze(), capacity()
903
\fn void QString::squeeze()
905
Releases any memory not required to store the character data.
907
The sole purpose of this function is to provide a means of fine
908
tuning QString's memory usage. In general, you will rarely ever
909
need to call this function.
911
\sa reserve(), capacity()
914
void QString::realloc(int alloc)
916
if (d->ref != 1 || d->data != d->array) {
917
Data *x = static_cast<Data *>(qMalloc(sizeof(Data) + alloc * sizeof(QChar)));
920
x->size = qMin(alloc, d->size);
921
::memcpy(x->array, d->data, x->size * sizeof(QChar));
922
x->array[x->size] = 0;
927
x->simpletext = d->simpletext;
928
x->righttoleft = d->righttoleft;
930
x = qAtomicSetPtr(&d, x);
936
Q_ASSERT(asciiCache);
937
asciiCache->remove(d);
940
Data *x = static_cast<Data *>(qRealloc(d, sizeof(Data) + alloc * sizeof(QChar)));
949
void QString::realloc()
954
void QString::expand(int i)
957
resize(qMax(i + 1, sz));
958
if (d->size - 1 > sz) {
959
ushort *n = d->data + d->size - 1;
960
ushort *e = d->data + sz;
966
/*! \fn void QString::clear()
968
Clears the contents of the string and makes it empty.
970
\sa resize(), isEmpty()
973
/*! \fn QString &QString::operator=(const QString &other)
975
Assigns \a other to this string and returns a reference to this
979
QString &QString::operator=(const QString &other)
983
x = qAtomicSetPtr(&d, x);
990
/*! \fn QString &QString::operator=(const QLatin1String &str)
994
Assigns the Latin-1 string \a str to this string.
997
/*! \fn QString &QString::operator=(const QByteArray &ba)
1001
Assigns \a ba to this string. \a ba is converted to Unicode using
1004
You can disable this operator by defining \c
1005
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1006
can be useful if you want to ensure that all user-visible strings
1007
go through QObject::tr(), for exaple.
1010
/*! \fn QString &QString::operator=(const char *str)
1014
Assigns \a str to this string. \a str is converted to Unicode
1017
You can disable this operator by defining \c
1018
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1019
can be useful if you want to ensure that all user-visible strings
1020
go through QObject::tr(), for example.
1023
/*! \fn QString &QString::operator=(char ch)
1027
Assigns character \a ch to this string. The character is converted
1028
to Unicode using fromAscii().
1030
You can disable this operator by defining \c
1031
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1032
can be useful if you want to ensure that all user-visible strings
1033
go through QObject::tr(), for example.
1039
Sets the string to contain the single character \a ch.
1041
QString &QString::operator=(QChar ch)
1043
return operator=(QString(ch));
1046
/*! \fn QString& QString::insert(int i, const QString &str)
1048
Inserts the string \a str at index position \a i and returns a
1049
reference to this string.
1053
QString str = "Meal";
1054
str.insert(1, QString("ontr"));
1055
// str == "Montreal"
1058
If \a i is greater than size(), the array is first extended using
1061
\sa append(), prepend(), replace(), remove()
1067
Inserts the Latin-1 string \a str at index position \a i.
1069
QString &QString::insert(int i, const QLatin1String &str)
1071
const uchar *s = (const uchar *)str.latin1();
1072
if (i < 0 || !s || !(*s))
1075
int len = qstrlen(str.latin1());
1076
expand(qMax(d->size, i) + len - 1);
1078
::memmove(d->data + i + len, d->data + i, (d->size - i - len) * sizeof(QChar));
1079
for (int j = 0; j < len; ++j)
1080
d->data[i + j] = s[j];
1086
Inserts the first \a size characters of the QChar array \a
1087
unicode at index position \a i in the string.
1089
QString& QString::insert(int i, const QChar *unicode, int size)
1091
if (i < 0 || size <= 0)
1094
const ushort *s = (const ushort *)unicode;
1095
if (s >= d->data && s < d->data + d->alloc) {
1096
// Part of me - take a copy
1097
ushort *tmp = static_cast<ushort *>(malloc(size * sizeof(QChar)));
1098
memcpy(tmp, s, size * sizeof(QChar));
1099
insert(i, reinterpret_cast<const QChar *>(tmp), size);
1104
expand(qMax(d->size, i) + size - 1);
1106
::memmove(d->data + i + size, d->data + i, (d->size - i - size) * sizeof(QChar));
1107
memcpy(d->data + i, s, size * sizeof(QChar));
1113
Inserts \a ch at index position \a i in the string.
1116
QString& QString::insert(int i, QChar ch)
1122
expand(qMax(i, d->size));
1123
::memmove(d->data + i + 1, d->data + i, (d->size - i) * sizeof(QChar));
1124
d->data[i] = ch.unicode();
1129
Appends the string \a str onto the end of this string.
1139
This is the same as insert(size(), \a str).
1141
This operation is typically very fast (\l{constant time}),
1142
because QString preallocates extra space at the end of the string
1143
data so it can grow without reallocating the entire string each
1146
\sa operator+=(), prepend(), insert()
1148
QString &QString::append(const QString &str)
1150
if (str.d != &shared_null) {
1151
if (d == &shared_null) {
1154
if (d->ref != 1 || d->size + str.d->size > d->alloc)
1155
realloc(grow(d->size + str.d->size));
1156
memcpy(d->data + d->size, str.d->data, str.d->size * sizeof(QChar));
1157
d->size += str.d->size;
1158
d->data[d->size] = '\0';
1166
Appends the Latin-1 string \a str to this string.
1168
QString &QString::append(const QLatin1String &str)
1170
const uchar *s = (const uchar *)str.latin1();
1172
int len = qstrlen((char *)s);
1173
if (d->ref != 1 || d->size + len > d->alloc)
1174
realloc(grow(d->size + len));
1175
ushort *i = d->data + d->size;
1176
while ((*i++ = *s++))
1183
/*! \fn QString &QString::append(const QByteArray &ba)
1187
Appends the byte array \a ba to this string. \a ba is converted
1188
to Unicode using fromAscii().
1190
You can disable this function by defining \c
1191
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1192
can be useful if you want to ensure that all user-visible strings
1193
go through QObject::tr(), for example.
1196
/*! \fn QString &QString::append(const char *str)
1200
Appends the string \a str to this string. \a str is converted to
1201
Unicode using fromAscii().
1203
You can disable this function by defining \c
1204
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1205
can be useful if you want to ensure that all user-visible strings
1206
go through QObject::tr(), for example.
1212
Appends the character \a ch to this string.
1214
QString &QString::append(QChar ch)
1216
if (d->ref != 1 || d->size + 1 > d->alloc)
1217
realloc(grow(d->size + 1));
1218
d->data[d->size++] = ch.unicode();
1219
d->data[d->size] = '\0';
1223
/*! \fn QString &QString::prepend(const QString &str)
1225
Prepends the string \a str to the beginning of this string and
1226
returns a reference to this string.
1236
\sa append(), insert()
1239
/*! \fn QString &QString::prepend(const QLatin1String &str)
1243
Prepends the Latin-1 string \a str to this string.
1246
/*! \fn QString &QString::prepend(const QByteArray &ba)
1250
Prepends the byte array \a ba to this string. \a ba is converted
1251
to Unicode using fromAscii().
1253
You can disable this function by defining \c
1254
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1255
can be useful if you want to ensure that all user-visible strings
1256
go through QObject::tr(), for example.
1259
/*! \fn QString &QString::prepend(const char *str)
1263
Prepends the string \a str to this string. \a str is converted to
1264
Unicode using fromAscii().
1266
You can disable this function by defining \c
1267
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1268
can be useful if you want to ensure that all user-visible strings
1269
go through QObject::tr(), for example.
1272
/*! \fn QString &QString::prepend(QChar ch)
1276
Prepends the character \a ch to this string.
1280
Removes \a len characters from the string, starting at index
1281
position \a pos, and returns a reference to the string.
1283
If \a pos is within the string, but \a pos + \a len is beyond the
1284
end of the string, the string is truncated at position \a pos.
1287
QString str = "Montreal";
1292
\sa insert(), replace()
1295
QString &QString::remove(int pos, int len)
1299
if (pos < 0 || pos >= d->size) {
1301
} else if (pos + len >= d->size) { // pos ok
1303
} else if (len > 0) {
1305
memmove(d->data + pos, d->data + pos + len,
1306
(d->size - pos - len + 1) * sizeof(ushort));
1314
Removes every occurrence of \a str in this string. Returns a
1315
reference to this string.
1317
If \a cs is Qt::CaseSensitive (the default), the search is
1318
case sensitive; otherwise the search is case insensitive.
1320
This is the same as replace(\a str, "", \a cs).
1322
QString &QString::remove(const QString &str, Qt::CaseSensitivity cs)
1326
while ((i = indexOf(str, i, cs)) != -1)
1327
remove(i, str.d->size);
1334
Removes every occurrence of the character \a ch in this string,
1335
and returns a reference to this string.
1337
If \a cs is Qt::CaseSensitive (the default), the search is
1338
case sensitive; otherwise the search is case insensitive.
1342
QString str = "Ali Baba";
1343
str.remove(QChar('a'), Qt::CaseInsensitive);
1347
This is the same as replace(\a ch, "", \a cs).
1349
QString &QString::remove(QChar ch, Qt::CaseSensitivity cs)
1352
if (cs == Qt::CaseSensitive) {
1354
if (*((const QChar*)d->data + i) == ch)
1361
if (::lower(*((const QChar*)d->data + i)) == ch)
1369
/*! \fn QString &QString::remove(const QRegExp &rx)
1373
Removes every occurrence of the regular expression \a rx in the
1374
string, and returns a reference to the string. For example:
1377
QString str = "Telephone";
1378
str.remove(QRegExp("[aeiou]."));
1382
\sa indexOf(), lastIndexOf(), replace()
1386
Replaces \a len characters from index position \a pos with the
1387
string \a after, and returns a reference to this string.
1391
QString x = "Say yes!";
1397
\sa insert(), remove()
1400
QString &QString::replace(int pos, int len, const QString &after)
1402
QString copy = after;
1404
return insert(pos, copy.constData(), copy.d->size);
1409
Replaces \a len characters from index position \a pos with the
1410
first \a size characters of the QChar array \a unicode.
1412
QString &QString::replace(int pos, int len, const QChar *unicode, int size)
1415
return insert(pos, unicode, size);
1421
Replaces \a len characters from index position \a pos with the
1424
QString &QString::replace(int pos, int len, QChar after)
1427
return insert(pos, after);
1432
Replaces every occurrence of the string \a before with the string
1435
If \a cs is Qt::CaseSensitive (the default), the search is
1436
case sensitive; otherwise the search is case insensitive.
1440
QString str = "colour behaviour flavour neighbour";
1441
str.replace(QString("ou"), QString("o"));
1442
// str == "color behavior flavor neighbor"
1445
QString &QString::replace(const QString &before, const QString &after, Qt::CaseSensitivity cs)
1451
if (cs == Qt::CaseSensitive && before == after)
1457
QStringMatcher matcher(before, cs);
1459
const int bl = before.d->size;
1460
const int al = after.d->size;
1464
const QChar *auc = (const QChar*) after.d->data;
1465
while ((index = matcher.indexIn(*this, index)) != -1) {
1466
// we need memmove(), not memcpy(), in the rare case where
1467
// this == &after, before == after, and cs is Qt::CaseInsensitive
1468
memmove(d->data + index, auc, al * sizeof(QChar));
1472
} else if (al < bl) {
1473
const QChar *auc = after.unicode();
1477
while ((index = matcher.indexIn(*this, index)) != -1) {
1479
int msize = index - movestart;
1481
memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
1488
memcpy(d->data+to, auc, al*sizeof(QChar));
1496
int msize = d->size - movestart;
1498
memmove(d->data + to, d->data + movestart, msize * sizeof(QChar));
1499
resize(d->size - num*(bl-al));
1502
const QString copy = after;
1504
// the most complex case. We don't want to lose performance by doing repeated
1505
// copies and reallocs of the string.
1506
while (index != -1) {
1509
while (pos < 4095) {
1510
index = matcher.indexIn(*this, index);
1513
indices[pos++] = index;
1515
// avoid infinite loop
1522
// we have a table of replacement positions, use them for fast replacing
1523
int adjust = pos*(al-bl);
1524
// index has to be adjusted in case we get back into the loop above.
1527
int newLen = d->size + adjust;
1528
int moveend = d->size;
1529
if (newLen > d->size)
1534
int movestart = indices[pos] + bl;
1535
int insertstart = indices[pos] + pos*(al-bl);
1536
int moveto = insertstart + al;
1537
memmove(d->data + moveto, d->data + movestart, (moveend - movestart)*sizeof(QChar));
1538
memcpy(d->data + insertstart, copy.unicode(), al*sizeof(QChar));
1539
moveend = movestart-bl;
1548
Replaces every occurrence of the character \a ch in the string
1549
with \a after. Returns a reference to the string.
1551
If \a cs is Qt::CaseSensitive (the default), the search is
1552
case sensitive; otherwise the search is case insensitive.
1555
QString& QString::replace(QChar ch, const QString &after, Qt::CaseSensitivity cs)
1557
return replace(QString(ch), after, cs);
1562
Replaces every occurrence of the character \a before with the
1563
character \a after. Returns a reference to the string.
1565
If \a cs is Qt::CaseSensitive (the default), the search is
1566
case sensitive; otherwise the search is case insensitive.
1568
QString& QString::replace(QChar before, QChar after, Qt::CaseSensitivity cs)
1572
QChar *e = i + d->size;
1573
if (cs == Qt::CaseSensitive) {
1578
before = ::lower(before);
1580
if (::lower(*i) == before)
1588
Returns true if string \a other is equal to this string;
1589
otherwise returns false.
1591
The comparison is based exclusively on the numeric Unicode values
1592
of the characters and is very fast, but is not what a human would
1593
expect. Consider sorting user-interface strings with
1594
localeAwareCompare().
1596
bool QString::operator==(const QString &other) const
1598
return (size() == other.size()) &&
1599
(memcmp((char*)unicode(),(char*)other.unicode(), size()*sizeof(QChar))==0);
1605
bool QString::operator==(const QLatin1String &other) const
1607
const ushort *uc = d->data;
1608
const ushort *e = uc + d->size;
1609
const uchar *c = (uchar *)other.latin1();
1615
if (uc == e || *uc != *c)
1623
/*! \fn bool QString::operator==(const QByteArray &other) const
1627
\a other is converted to a QString using fromAscii().
1629
You can disable this operator by defining \c
1630
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1631
can be useful if you want to ensure that all user-visible strings
1632
go through QObject::tr(), for example.
1635
/*! \fn bool QString::operator==(const char *other) const
1639
\a other is converted to a QString using fromAscii().
1641
You can disable this operator by defining \c
1642
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1643
can be useful if you want to ensure that all user-visible strings
1644
go through QObject::tr(), for example.
1648
Returns true if this string is lexically less than string \a
1649
other; otherwise returns false.
1651
The comparison is based exclusively on the numeric Unicode values
1652
of the characters and is very fast, but is not what a human would
1653
expect. Consider sorting user-interface strings with
1654
localeAwareCompare().
1656
bool QString::operator<(const QString &other) const
1658
return ucstrcmp(*this, other) < 0;
1664
bool QString::operator<(const QLatin1String &other) const
1666
const ushort *uc = d->data;
1667
const ushort *e = uc + d->size;
1668
const uchar *c = (uchar *) other.latin1();
1674
if (uc == e || *uc != *c)
1679
return (uc == e ? *c : *uc < *c);
1682
/*! \fn bool QString::operator<(const QByteArray &other) const
1686
\a other is converted to a QString using fromAscii().
1688
You can disable this operator by defining \c
1689
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1690
can be useful if you want to ensure that all user-visible strings
1691
go through QObject::tr(), for example.
1694
/*! \fn bool QString::operator<(const char *other) const
1698
\a other is converted to a QString using fromAscii().
1700
You can disable this operator by defining \c
1701
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1702
can be useful if you want to ensure that all user-visible strings
1703
go through QObject::tr(), for example.
1706
/*! \fn bool QString::operator<=(const QString &other) const
1708
Returns true if this string is lexically less than or equal to
1709
string \a other; otherwise returns false.
1711
The comparison is based exclusively on the numeric Unicode values
1712
of the characters and is very fast, but is not what a human would
1713
expect. Consider sorting user-interface strings with
1714
localeAwareCompare().
1717
/*! \fn bool QString::operator<=(const QLatin1String &other) const
1722
/*! \fn bool QString::operator<=(const QByteArray &other) const
1726
\a other is converted to a QString using fromAscii().
1728
You can disable this operator by defining \c
1729
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1730
can be useful if you want to ensure that all user-visible strings
1731
go through QObject::tr(), for example.
1734
/*! \fn bool QString::operator<=(const char *other) const
1738
\a other is converted to a QString using fromAscii().
1740
You can disable this operator by defining \c
1741
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1742
can be useful if you want to ensure that all user-visible strings
1743
go through QObject::tr(), for example.
1746
/*! \fn bool QString::operator>(const QString &other) const
1748
Returns true if this string is lexically greater than string \a
1749
other; otherwise returns false.
1751
The comparison is based exclusively on the numeric Unicode values
1752
of the characters and is very fast, but is not what a human would
1753
expect. Consider sorting user-interface strings with
1754
localeAwareCompare().
1760
bool QString::operator>(const QLatin1String &other) const
1762
const ushort *uc = d->data;;
1763
const ushort *e = uc + d->size;
1764
const uchar *c = (uchar *) other.latin1();
1766
if (!c || *c == '\0')
1770
if (uc == e || *uc != *c)
1775
return (uc == e ? false : *uc > *c);
1778
/*! \fn bool QString::operator>(const QByteArray &other) const
1782
\a other is converted to a QString using fromAscii().
1784
You can disable this operator by defining \c
1785
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1786
can be useful if you want to ensure that all user-visible strings
1787
go through QObject::tr(), for example.
1790
/*! \fn bool QString::operator>(const char *other) const
1794
\a other is converted to a QString using fromAscii().
1796
You can disable this operator by defining \c
1797
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1798
can be useful if you want to ensure that all user-visible strings
1799
go through QObject::tr(), for example.
1802
/*! \fn bool QString::operator>=(const QString &other) const
1804
Returns true if this string is lexically greater than or equal to
1805
string \a other; otherwise returns false.
1807
The comparison is based exclusively on the numeric Unicode values
1808
of the characters and is very fast, but is not what a human would
1809
expect. Consider sorting user-interface strings with
1810
localeAwareCompare().
1813
/*! \fn bool QString::operator>=(const QLatin1String &other) const
1818
/*! \fn bool QString::operator>=(const QByteArray &other) const
1822
\a other is converted to a QString using fromAscii().
1824
You can disable this operator by defining \c
1825
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1826
can be useful if you want to ensure that all user-visible strings
1827
go through QObject::tr(), for example.
1830
/*! \fn bool QString::operator>=(const char *other) const
1834
\a other is converted to a QString using fromAscii().
1836
You can disable this operator by defining \c
1837
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1838
can be useful if you want to ensure that all user-visible strings
1839
go through QObject::tr(), for example.
1842
/*! \fn bool QString::operator!=(const QString &other) const
1844
Returns true if this string is not equal to string \a other;
1845
otherwise returns false.
1847
The comparison is based exclusively on the numeric Unicode values
1848
of the characters and is very fast, but is not what a human would
1849
expect. Consider sorting user-interface strings with
1850
localeAwareCompare().
1853
/*! \fn bool QString::operator!=(const QLatin1String &other) const
1858
/*! \fn bool QString::operator!=(const QByteArray &other) const
1862
\a other is converted to a QString using fromAscii().
1864
You can disable this operator by defining \c
1865
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1866
can be useful if you want to ensure that all user-visible strings
1867
go through QObject::tr(), for example.
1870
/*! \fn bool QString::operator!=(const char *other) const
1874
\a other is converted to a QString using fromAscii().
1876
You can disable this operator by defining \c
1877
QT_NO_CAST_FROM_ASCII when you compile your applications. This
1878
can be useful if you want to ensure that all user-visible strings
1879
go through QObject::tr(), for example.
1883
if (sl_minus_1 < (int)sizeof(int) * CHAR_BIT) \
1884
hashHaystack -= (a) << sl_minus_1; \
1888
Returns the index position of the first occurrence of the string
1889
\a str in this string, searching forward from index position \a
1890
from. Returns -1 if \a str is not found.
1892
If \a cs is Qt::CaseSensitive (the default), the search is
1893
case sensitive; otherwise the search is case insensitive.
1897
QString x = "sticky question";
1899
x.indexOf(y); // returns 0
1900
x.indexOf(y, 1); // returns 10
1901
x.indexOf(y, 10); // returns 10
1902
x.indexOf(y, 11); // returns -1
1905
If \a from is -1, the search starts at the last character; if it
1906
is -2, at the next to last character and so on.
1908
\sa lastIndexOf(), contains(), count()
1910
int QString::indexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
1912
const int l = d->size;
1913
const int sl = str.d->size;
1924
return indexOf(*(const QChar *)str.d->data, from, cs);
1927
We use the Boyer-Moore algorithm in cases where the overhead
1928
for the skip table should pay off, otherwise we use a simple
1931
if (l > 500 && sl > 5)
1932
return QStringMatcher(str, cs).indexIn(*this, from);
1935
We use some hashing for efficiency's sake. Instead of
1936
comparing strings, we compare the hash value of str with that
1937
of a part of this QString. Only if that matches, we call
1938
ucstrncmp() or ucstrnicmp().
1940
const QChar *needle = (const QChar*) str.d->data;
1941
const QChar *haystack = (const QChar*) d->data + from;
1942
const QChar *end = (const QChar*) d->data + (l-sl);
1943
const int sl_minus_1 = sl-1;
1944
int hashNeedle = 0, hashHaystack = 0, idx;
1946
if (cs == Qt::CaseSensitive) {
1947
for (idx = 0; idx < sl; ++idx) {
1948
hashNeedle = ((hashNeedle<<1) + needle[idx].unicode());
1949
hashHaystack = ((hashHaystack<<1) + haystack[idx].unicode());
1951
hashHaystack -= (haystack+sl_minus_1)->unicode();
1953
while (haystack <= end) {
1954
hashHaystack += (haystack+sl_minus_1)->unicode();
1955
if (hashHaystack == hashNeedle
1956
&& ucstrncmp(needle, haystack, sl) == 0)
1957
return haystack-unicode();
1959
REHASH(haystack->unicode());
1963
for (idx = 0; idx < sl; ++idx) {
1964
hashNeedle = ((hashNeedle<<1) +
1965
::lower(needle[idx].unicode()).unicode());
1966
hashHaystack = ((hashHaystack<<1) +
1967
::lower(haystack[idx].unicode()).unicode());
1970
hashHaystack -= ::lower(*(haystack+sl_minus_1)).unicode();
1971
while (haystack <= end) {
1972
hashHaystack += ::lower(*(haystack+sl_minus_1)).unicode();
1973
if (hashHaystack == hashNeedle
1974
&& ucstrnicmp(needle, haystack, sl) == 0)
1975
return haystack-unicode();
1977
REHASH(::lower(*haystack).unicode());
1987
Returns the index position of the first occurrence of the
1988
character \a ch in the string, searching forward from index
1989
position \a from. Returns -1 if \a ch could not be found.
1991
int QString::indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
1994
from = qMax(from + d->size, 0);
1995
if (from < d->size) {
1996
const QChar *n = (const QChar*)d->data + from - 1;
1997
const QChar *e = (const QChar*)d->data + d->size;
1998
if (cs == Qt::CaseSensitive) {
2001
return n - (const QChar*)d->data;
2005
if (::lower(*n) == ch)
2006
return n - (const QChar*)d->data;
2013
Returns the index position of the last occurrence of the string
2014
\a str in this string, searching backward from index position \a
2015
from. If \a from is -1 (the default), the search starts at the
2016
last character; if \a from is -2, at the next to last character
2017
and so on. Returns -1 if \a str is not found.
2019
If \a cs is Qt::CaseSensitive (the default), the search is
2020
case sensitive; otherwise the search is case insensitive.
2024
QString x = "crazy azimuths";
2026
x.lastIndexOf(y); // returns 6
2027
x.lastIndexOf(y, 6); // returns 6
2028
x.lastIndexOf(y, 5); // returns 2
2029
x.lastIndexOf(y, 1); // returns -1
2032
\sa indexOf(), contains(), count()
2034
int QString::lastIndexOf(const QString &str, int from, Qt::CaseSensitivity cs) const
2037
See indexOf() for explanations.
2039
const int l = d->size;
2042
const int sl = str.d->size;
2044
if (from < 0 || from >= l || delta < 0)
2050
return lastIndexOf(*(const QChar*) str.d->data, from, cs);
2052
const QChar *needle = (const QChar*) str.d->data;
2053
const QChar *haystack = (const QChar*) d->data + from;
2054
const QChar *end = (const QChar*) d->data;
2055
const int sl_minus_1 = sl-1;
2056
const QChar *n = needle+sl_minus_1;
2057
const QChar *h = haystack+sl_minus_1;
2058
int hashNeedle = 0, hashHaystack = 0, idx;
2060
if (cs == Qt::CaseSensitive) {
2061
for (idx = 0; idx < sl; ++idx) {
2062
hashNeedle = ((hashNeedle<<1) + (n-idx)->unicode());
2063
hashHaystack = ((hashHaystack<<1) + (h-idx)->unicode());
2065
hashHaystack -= haystack->unicode();
2067
while (haystack >= end) {
2068
hashHaystack += haystack->unicode();
2069
if (hashHaystack == hashNeedle
2070
&& ucstrncmp(needle, haystack, sl) == 0)
2071
return haystack-unicode();
2073
REHASH((haystack+sl)->unicode());
2076
for (idx = 0; idx < sl; ++idx) {
2077
hashNeedle = ((hashNeedle<<1)
2078
+ ::lower((n-idx)->unicode()).unicode());
2079
hashHaystack = ((hashHaystack<<1)
2080
+ ::lower((h-idx)->unicode()).unicode());
2082
hashHaystack -= ::lower(*haystack).unicode();
2084
while (haystack >= end) {
2085
hashHaystack += ::lower(*haystack).unicode();
2086
if (hashHaystack == hashNeedle
2087
&& ucstrnicmp(needle, haystack, sl) == 0)
2088
return haystack-unicode();
2090
REHASH(::lower(*(haystack+sl)).unicode());
2098
Returns the index position of the last occurrence of the
2099
character \a ch, searching backward from position \a from.
2101
int QString::lastIndexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
2105
if (from < 0 || from >= d->size)
2108
const QChar *n = (const QChar*)d->data + from;
2109
const QChar *b = (const QChar*)d->data;
2110
if (cs == Qt::CaseSensitive) {
2117
if (::lower(*n) == ch)
2124
#ifndef QT_NO_REGEXP_CAPTURE
2127
Replaces every occurrence of the regular expression \a rx in the
2128
string with \a after. Returns a reference to the string. For
2131
QString str = "Banana";
2132
str.replace(QRegExp("a[mn]"), "ox");
2136
For regular expressions containing \l{capturing parentheses},
2137
occurrences of \bold{\\1}, \bold{\\2}, ..., in \a after are
2138
replaced with \a{rx}.cap(1), cap(2), ...
2141
QString str = "A <i>bon mot</i>.";
2142
str.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}");
2143
// str == "A \\emph{bon mot}."
2146
\sa indexOf(), lastIndexOf(), remove(), QRegExp::cap()
2148
QString& QString::replace(const QRegExp &rx, const QString &after)
2152
if (isEmpty() && rx2.indexIn(*this) == -1)
2158
int numCaptures = rx2.numCaptures();
2159
int al = after.length();
2160
QRegExp::CaretMode caretMode = QRegExp::CaretAtZero;
2162
if (numCaptures > 0) {
2163
if (numCaptures > 9)
2166
const QChar *uc = after.unicode();
2167
int numBackRefs = 0;
2169
for (int i = 0; i < al - 1; i++) {
2170
if (uc[i] == QLatin1Char('\\')) {
2171
int no = uc[i + 1].digitValue();
2172
if (no > 0 && no <= numCaptures)
2178
This is the harder case where we have back-references.
2179
We don't try to optimize it.
2181
if (numBackRefs > 0) {
2182
int *capturePositions = new int[numBackRefs];
2183
int *captureNumbers = new int[numBackRefs];
2186
for (int i = 0; i < al - 1; i++) {
2187
if (uc[i] == QLatin1Char('\\')) {
2188
int no = uc[i + 1].digitValue();
2189
if (no > 0 && no <= numCaptures) {
2190
capturePositions[j] = i;
2191
captureNumbers[j] = no;
2197
while (index <= length()) {
2198
index = rx2.indexIn(*this, index, caretMode);
2202
QString after2 = after;
2203
for (j = numBackRefs - 1; j >= 0; j--)
2204
after2.replace(capturePositions[j], 2,
2205
rx2.cap(captureNumbers[j]));
2207
replace(index, rx2.matchedLength(), after2);
2208
index += after2.length();
2210
if (rx2.matchedLength() == 0) {
2211
// avoid infinite loop on 0-length matches (e.g., [a-z]*)
2214
caretMode = QRegExp::CaretWontMatch;
2216
delete[] capturePositions;
2217
delete[] captureNumbers;
2223
This is the simple and optimized case where we don't have
2226
while (index != -1) {
2230
} replacements[2048];
2234
while (pos < 2047) {
2235
index = rx2.indexIn(*this, index, caretMode);
2238
int ml = rx2.matchedLength();
2239
replacements[pos].pos = index;
2240
replacements[pos++].length = ml;
2243
// avoid infinite loop
2249
replacements[pos].pos = d->size;
2250
int newlen = d->size + adjust;
2252
// to continue searching at the right position after we did
2253
// the first round of replacements
2257
newstring.reserve(newlen + 1);
2258
QChar *newuc = newstring.data();
2263
int copyend = replacements[i].pos;
2264
int size = copyend - copystart;
2265
memcpy(uc, d->data + copystart, size * sizeof(QChar));
2267
memcpy(uc, after.d->data, al * sizeof(QChar));
2269
copystart = copyend + replacements[i].length;
2272
memcpy(uc, d->data + copystart, (d->size - copystart) * sizeof(QChar));
2273
newstring.resize(newlen);
2275
caretMode = QRegExp::CaretWontMatch;
2282
Returns the number of (potentially overlapping) occurrences of
2283
the string \a str in this string.
2285
If \a cs is Qt::CaseSensitive (the default), the search is
2286
case sensitive; otherwise the search is case insensitive.
2288
\sa contains(), indexOf()
2290
int QString::count(const QString &str, Qt::CaseSensitivity cs) const
2294
if (d->size > 500 && str.d->size > 5) {
2295
QStringMatcher matcher(str, cs);
2296
while ((i = matcher.indexIn(*this, i + 1)) != -1)
2299
while ((i = indexOf(str, i + 1, cs)) != -1)
2307
Returns the number of occurrences of character \a ch in the
2310
int QString::count(QChar ch, Qt::CaseSensitivity cs) const
2313
const QChar *i = (const QChar*) d->data + d->size;
2314
const QChar *b = (const QChar*) d->data;
2315
if (cs == Qt::CaseSensitive) {
2322
if (::lower(*--i) == ch)
2328
/*! \fn bool QString::contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
2330
Returns true if this string contains an occurrence of the string
2331
\a str; otherwise returns false.
2333
If \a cs is Qt::CaseSensitive (the default), the search is
2334
case sensitive; otherwise the search is case insensitive.
2338
QString str = "Peter Pan";
2339
str.contains("peter", Qt::CaseInsensitive); // returns true
2342
\sa indexOf(), count()
2345
/*! \fn bool QString::contains(QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
2349
Returns true if this string contains an occurrence of the
2350
character \a ch; otherwise returns false.
2353
/*! \fn bool QString::contains(const QRegExp &rx) const
2357
Returns true if the regular expression \a rx matches somewhere in
2358
this string; otherwise returns false.
2361
#ifndef QT_NO_REGEXP
2365
Returns the index position of the first match of the regular
2366
expression \a rx in the string, searching forward from index
2367
position \a from. Returns -1 if \a rx didn't match anywhere.
2371
QString str = "the minimum";
2372
str.indexOf(QRegExp("m[aeiou]"), 0); // returns 4
2375
int QString::indexOf(const QRegExp& rx, int from) const
2377
return rx.indexIn(*this, from);
2383
Returns the index position of the last match of the regular
2384
expression \a rx in the string, searching backward from index
2385
position \a from. Returns -1 if \a rx didn't match anywhere.
2389
QString str = "the minimum";
2390
str.lastIndexOf(QRegExp("m[aeiou]")); // returns 8
2393
int QString::lastIndexOf(const QRegExp& rx, int from) const
2395
return rx.lastIndexIn(*this, from);
2400
Returns the number of times the regular expression \a rx matches
2403
This function counts overlapping matches, so in the example
2404
below, there are four instances of "ana" or "ama":
2407
QString str = "banana and panama";
2408
str.contains(QRegExp("a[nm]a")); // returns 4
2411
int QString::count(const QRegExp& rx) const
2416
while (index < len - 1) { // count overlapping matches
2417
index = rx.indexIn(*this, index + 1);
2424
#endif // QT_NO_REGEXP
2426
/*! \fn int QString::count() const
2435
\enum QString::SectionFlag
2437
\value SectionDefault Empty fields are counted, leading and
2438
trailing separators are not included, and the separator is
2439
compared case sensitively.
2441
\value SectionSkipEmpty Treat empty fields as if they don't exist,
2442
i.e. they are not considered as far as \e start and \e end are
2445
\value SectionIncludeLeadingSep Include the leading separator (if
2446
any) in the result string.
2448
\value SectionIncludeTrailingSep Include the trailing separator
2449
(if any) in the result string.
2451
\value SectionCaseInsensitiveSeps Compare the separator
2458
\fn QString QString::section(QChar sep, int start, int end = -1, SectionFlags flags) const
2460
This function returns a section of the string.
2462
This string is treated as a sequence of fields separated by the
2463
character, \a sep. The returned string consists of the fields from
2464
position \a start to position \a end inclusive. If \a end is not
2465
specified, all fields from position \a start to the end of the
2466
string are included. Fields are numbered 0, 1, 2, etc., counting
2467
from the left, and -1, -2, etc., counting from right to left.
2469
The \a flags argument can be used to affect some aspects of the
2470
function's behavior, e.g. whether to be case sensitive, whether
2471
to skip empty fields and how to deal with leading and trailing
2472
separators; see \l{SectionFlags}.
2475
QString csv = "forename,middlename,surname,phone";
2476
QString str = csv.section(',', 2, 2); // str == "surname"
2478
QString path = "/usr/local/bin/myapp"; // First field is empty
2479
QString str = path.section('/', 3, 4); // str == "bin/myapp"
2480
QString str = path.section('/', 3, 3, SectionSkipEmpty); // str == "myapp"
2483
If \a start or \a end is negative, we count fields from the right
2484
of the string, the right-most field being -1, the one from
2485
right-most field being -2, and so on.
2488
QString csv = "forename,middlename,surname,phone";
2489
QString str = csv.section(',', -3, -2); // str == "middlename,surname"
2491
QString path = "/usr/local/bin/myapp"; // First field is empty
2492
QString str = path.section('/', -1); // str == "myapp"
2502
QString data = "forename**middlename**surname**phone";
2503
QString str = data.section("**", 2, 2); // str == "surname"
2507
QString data = "forename**middlename**surname**phone";
2508
QString str = data.section("**", -3, -2); // str == "middlename**surname"
2514
QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
2516
QStringList sections = split(sep, KeepEmptyParts,
2517
(flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
2518
if (sections.isEmpty())
2520
if (!(flags & SectionSkipEmpty)) {
2522
start += sections.count();
2524
end += sections.count();
2527
for (int k=0; k<sections.size(); ++k) {
2528
if (sections.at(k).isEmpty())
2532
start += sections.count() - skip;
2534
end += sections.count() - skip;
2538
int first_i = start, last_i = end;
2539
for (int i = 0; x <= end && i < sections.size(); ++i) {
2540
QString section = sections.at(i);
2541
const bool empty = section.isEmpty();
2551
if (!empty || !(flags & SectionSkipEmpty))
2554
if((flags & SectionIncludeLeadingSep) && first_i)
2556
if((flags & SectionIncludeTrailingSep) && last_i < sections.size()-1)
2561
#ifndef QT_NO_REGEXP
2562
class section_chunk {
2564
section_chunk(int l, QString s) { length = l; string = s; }
2572
This string is treated as a sequence of fields separated by the
2573
regular expression, \a reg.
2576
QString line = "forename\tmiddlename surname \t \t phone";
2577
QRegExp sep("\\s+");
2578
QString s = line.section(sep, 2, 2); // s == "surname"
2582
QString line = "forename\tmiddlename surname \t \t phone";
2583
QRegExp sep("\\s+");
2584
QString s = line.section(sep, -3, -2); // s == "middlename surname"
2587
\warning Using this QRegExp version is much more expensive than
2588
the overloaded string and character versions.
2590
\sa split() simplified()
2592
QString QString::section(const QRegExp ®, int start, int end, SectionFlags flags) const
2594
const QChar *uc = unicode();
2599
sep.setCaseSensitivity((flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive
2600
: Qt::CaseSensitive);
2602
QList<section_chunk> sections;
2603
int n = length(), m = 0, last_m = 0, last_len = 0;
2604
while ((m = sep.indexIn(*this, m)) != -1) {
2605
sections.append(section_chunk(last_len, QString(uc + last_m, m - last_m)));
2607
last_len = sep.matchedLength();
2608
m += sep.matchedLength();
2610
sections.append(section_chunk(last_len, QString(uc + last_m, n - last_m)));
2613
start += sections.count();
2615
end += sections.count();
2619
int first_i = start, last_i = end;
2620
for (int i = 0; x <= end && i < sections.size(); ++i) {
2621
const section_chunk §ion = sections.at(i);
2622
const bool empty = (section.length == section.string.length());
2629
ret += section.string;
2631
ret += section.string.mid(section.length);
2633
if (!empty || !(flags & SectionSkipEmpty))
2636
if((flags & SectionIncludeLeadingSep)) {
2637
const section_chunk §ion = sections.at(first_i);
2638
ret.prepend(section.string.left(section.length));
2640
if((flags & SectionIncludeTrailingSep) && last_i+1 <= sections.size()-1) {
2641
const section_chunk §ion = sections.at(last_i+1);
2642
ret += section.string.left(section.length);
2649
Returns a substring that contains the \a len leftmost characters
2652
The entire string is returned if \a len is greater than size() or
2656
QString x = "Pineapple";
2657
QString y = x.left(4); // y == "Pine"
2660
\sa right(), mid(), startsWith()
2662
QString QString::left(int len) const
2664
if (d == &shared_null)
2666
if (len > d->size || len < 0)
2668
return QString((const QChar*) d->data, len);
2672
Returns a substring that contains the \a len rightmost characters
2675
The entire string is returned if \a len is greater than size() or
2679
QString x = "Pineapple";
2680
QString y = x.right(5); // y == "apple"
2683
\sa left(), mid(), endsWith()
2685
QString QString::right(int len) const
2687
if (d == &shared_null)
2689
if (len > d->size || len < 0)
2691
return QString((const QChar*) d->data + d->size - len, len);
2695
Returns a string that contains the \a len characters of this
2696
string, starting at position \a i.
2698
Returns an empty string if index \a i exceeds the length of the
2699
string. If there are less than \a len characters available in the
2700
string starting at position \a i, or if \a len is -1 (the
2701
default), the function returns all characters that are available
2706
QString x = "Nine pineapples";
2707
QString y = x.mid(5, 4); // y == "pine"
2708
QString z = x.mid(5); // z == "pineapples"
2714
QString QString::mid(int i, int len) const
2716
if (d == &shared_null || i >= d->size)
2724
if (len + i > d->size)
2726
if (i == 0 && len == d->size)
2728
return QString((const QChar*) d->data + i, len);
2732
Returns true if the string starts with \a s; otherwise returns
2735
If \a cs is Qt::CaseSensitive (the default), the search is
2736
case sensitive; otherwise the search is case insensitive.
2739
QString str = "Bananas";
2740
str.startsWith("Ban"); // returns true
2741
str.startsWith("Car"); // returns false
2746
bool QString::startsWith(const QString& s, Qt::CaseSensitivity cs) const
2748
if (d == &shared_null)
2749
return (s.d == &shared_null);
2751
return s.d->size == 0;
2752
if (s.d->size > d->size)
2754
if (cs == Qt::CaseSensitive) {
2755
return memcmp((char*)d->data, (char*)s.d->data, s.d->size*sizeof(QChar)) == 0;
2757
for (int i = 0; i < s.d->size; ++i)
2758
if (::lower(d->data[i]) != ::lower(s.d->data[i]))
2767
bool QString::startsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const
2769
if (d == &shared_null)
2770
return (s.latin1() == 0);
2772
return !s.latin1() || *s.latin1() == 0;
2773
int slen = qstrlen(s.latin1());
2776
const uchar *latin = (const uchar *)s.latin1();
2777
if (cs == Qt::CaseSensitive) {
2778
for (int i = 0; i < slen; ++i)
2779
if (d->data[i] != latin[i])
2782
for (int i = 0; i < slen; ++i)
2783
if (QUnicodeTables::lower(d->data[i]) != QUnicodeTables::lower(latin[i]))
2792
bool QString::startsWith(const QChar &c, Qt::CaseSensitivity cs) const
2795
&& cs == Qt::CaseSensitive
2797
: QUnicodeTables::lower(d->data[0]) == QUnicodeTables::lower(c.unicode());
2801
Returns true if the string ends with \a s; otherwise returns
2804
If \a cs is Qt::CaseSensitive (the default), the search is case
2805
sensitive; otherwise the search is case insensitive.
2808
QString str = "Bananas";
2809
str.endsWith("anas"); // returns true
2810
str.endsWith("pple"); // returns false
2815
bool QString::endsWith(const QString& s, Qt::CaseSensitivity cs) const
2817
if (d == &shared_null)
2818
return (s.d == &shared_null);
2820
return s.d->size == 0;
2821
int pos = d->size - s.d->size;
2824
if (cs == Qt::CaseSensitive) {
2825
return memcmp((char*)&d->data[pos], (char*)s.d->data, s.d->size*sizeof(QChar)) == 0;
2827
for (int i = 0; i < s.length(); i++)
2828
if (::lower(d->data[pos+i]) != ::lower(s.d->data[i]))
2837
bool QString::endsWith(const QLatin1String& s, Qt::CaseSensitivity cs) const
2839
if (d == &shared_null)
2840
return (s.latin1() == 0);
2842
return !s.latin1() || *s.latin1() == 0;
2843
int slen = qstrlen(s.latin1());
2844
int pos = d->size - slen;
2845
const uchar *latin = (const uchar *)s.latin1();
2848
if (cs == Qt::CaseSensitive) {
2849
for (int i = 0; i < slen; i++)
2850
if (d->data[pos+i] != latin[i])
2853
for (int i = 0; i < slen; i++)
2854
if (QUnicodeTables::lower(d->data[pos+i]) != QUnicodeTables::lower(latin[i]))
2863
bool QString::endsWith(const QChar &c, Qt::CaseSensitivity cs) const
2866
&& cs == Qt::CaseSensitive
2867
? d->data[d->size - 1] == c
2868
: QUnicodeTables::lower(d->data[d->size - 1]) == QUnicodeTables::lower(c.unicode());
2871
/*! \fn const char *QString::ascii() const
2873
Use toAscii() instead.
2876
/*! \fn const char *QString::latin1() const
2878
Use toLatin1() instead.
2881
/*! \fn const char *QString::utf8() const
2883
Use toUtf8() instead.
2886
/*! \fn const char *QString::local8Bit() const
2888
Use toLocal8Bit() instead.
2892
Returns a Latin-1 representation of the string as a QByteArray.
2893
The returned byte array is undefined if the string contains
2894
non-Latin1 characters.
2896
\sa fromLatin1(), toAscii(), toUtf8(), toLocal8Bit(), QTextCodec
2898
QByteArray QString::toLatin1() const
2903
const ushort *i = d->data;
2904
const ushort *e = d->data + d->size;
2905
uchar *s = (uchar*) ba.data();
2907
*s++ = (*i>0xff) ? '?' : (uchar) *i;
2915
Returns an 8-bit ASCII representation of the string as a QByteArray.
2917
If a codec has been set using QTextCodec::setCodecForCStrings(),
2918
it is used to convert Unicode to 8-bit char; otherwise this
2919
function does the same as toLatin1().
2921
\sa fromAscii(), toLatin1(), toUtf8(), toLocal8Bit(), QTextCodec
2923
QByteArray QString::toAscii() const
2925
#ifndef QT_NO_TEXTCODEC
2926
if (codecForCStrings)
2927
return codecForCStrings->fromUnicode(*this);
2928
#endif // QT_NO_TEXTCODEC
2933
Returns the local 8-bit representation of the string as a
2934
QByteArray. The returned byte array is undefined if the string
2935
contains characters not supported by the local 8-bit encoding.
2937
QTextCodec::codecForLocale() is used to perform the conversion
2940
\sa fromLocal8Bit(), toAscii(), toLatin1(), toUtf8(), QTextCodec
2942
QByteArray QString::toLocal8Bit() const
2944
#ifndef QT_NO_TEXTCODEC
2945
if (QTextCodec::codecForLocale())
2946
return QTextCodec::codecForLocale()->fromUnicode(*this);
2947
#endif // QT_NO_TEXTCODEC
2952
Returns a UTF-8 representation of the string as a QByteArray.
2954
\sa fromUtf8(), toAscii(), toLatin1(), toLocal8Bit(), QTextCodec
2956
QByteArray QString::toUtf8() const
2963
uchar *cursor = (uchar*)ba.data();
2964
const ushort *ch =d->data;
2965
for (int i=0; i < l; i++) {
2968
*cursor++ = (uchar)u;
2971
*cursor++ = 0xc0 | ((uchar) (u >> 6));
2973
if (u >= 0xd800 && u < 0xdc00 && i < l-1) {
2975
if (low >= 0xdc00 && low < 0xe000) {
2978
u = (u - 0xd800)*0x400 + (low - 0xdc00) + 0x10000;
2982
// if people are working in utf8, but strings are encoded in eg. latin1, the resulting
2983
// name might be invalid utf8. This and the corresponding code in fromUtf8 takes care
2984
// we can handle this without loosing information. This can happen with latin filenames
2985
// and a utf8 locale under Unix.
2986
if (u > 0x10fe00 && u < 0x10ff00) {
2987
*cursor++ = (u - 0x10fe00);
2991
*cursor++ = 0xf0 | ((uchar) (u >> 18));
2992
*cursor++ = 0x80 | (((uchar) (u >> 12)) & 0x3f);
2995
*cursor++ = 0xe0 | ((uchar) (u >> 12));
2997
*cursor++ = 0x80 | (((uchar) (u >> 6)) & 0x3f);
2999
*cursor++ = 0x80 | ((uchar) (u&0x3f));
3003
ba.resize(cursor - (uchar*)ba.constData());
3009
Returns a QString initialized with the first \a size characters
3010
of the Latin-1 string \a str.
3012
If \a size is -1 (the default), it is taken to be qstrlen(\a
3015
\sa toLatin1(), fromAscii(), fromUtf8(), fromLocal8Bit()
3017
QString QString::fromLatin1(const char *str, int size)
3023
} else if (size == 0 || (!*str && size < 0)) {
3028
size = qstrlen(str);
3029
d = static_cast<Data *>(qMalloc(sizeof(Data) + size * sizeof(QChar)));
3031
d->alloc = d->size = size;
3032
d->clean = d->asciiCache = d->simpletext = d->righttoleft = 0;
3034
ushort *i = d->data;
3035
d->array[size] = '\0';
3037
*i++ = (uchar)*str++;
3039
return QString(d, 0);
3048
const char *QString::ascii_helper() const
3051
asciiCache = new QHash<void *, QByteArray>();
3053
d->asciiCache = true;
3054
QByteArray ascii = toAscii();
3055
QByteArray old = asciiCache->value(d);
3057
return old.constData();
3058
asciiCache->insert(d, ascii);
3059
return ascii.constData();
3065
const char *QString::latin1_helper() const
3068
asciiCache = new QHash<void *, QByteArray>();
3070
d->asciiCache = true;
3071
QByteArray ascii = toLatin1();
3072
QByteArray old = asciiCache->value(d);
3074
return old.constData();
3075
asciiCache->insert(d, ascii);
3076
return ascii.constData();
3082
#include "qt_windows.h"
3084
QByteArray qt_winQString2MB(const QString& s, int uclen)
3089
return QByteArray();
3091
return QByteArray("");
3092
return qt_winQString2MB(s.constData(), uclen);
3095
QByteArray qt_winQString2MB(const QChar *ch, int uclen)
3098
return QByteArray();
3100
return QByteArray("");
3102
QByteArray mb(4096, 0);
3104
while (!(len=WideCharToMultiByte(CP_ACP, 0, (const WCHAR*)ch, uclen,
3105
mb.data(), mb.size()-1, 0, &used_def)))
3107
int r = GetLastError();
3108
if (r == ERROR_INSUFFICIENT_BUFFER) {
3109
mb.resize(1+WideCharToMultiByte(CP_ACP, 0,
3110
(const WCHAR*)ch, uclen,
3111
0, 0, 0, &used_def));
3116
qWarning("WideCharToMultiByte cannot convert multibyte text (error %d): %s (UTF-8)",
3117
r, QString(ch, uclen).toLocal8Bit().data());
3126
QString qt_winMB2QString(const char *mb, int mblen)
3130
const int wclen_auto = 4096;
3131
WCHAR wc_auto[wclen_auto];
3132
int wclen = wclen_auto;
3133
WCHAR *wc = wc_auto;
3135
while (!(len=MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
3136
mb, mblen, wc, wclen)))
3138
int r = GetLastError();
3139
if (r == ERROR_INSUFFICIENT_BUFFER) {
3140
if (wc != wc_auto) {
3141
qWarning("Size changed in MultiByteToWideChar");
3144
wclen = MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED,
3146
wc = new WCHAR[wclen];
3151
qWarning("MultiByteToWideChar cannot convert multibyte text");
3157
if (wc[len-1] == 0) // len - 1: we don't want terminator
3159
QString s((QChar*)wc, len);
3164
#endif // Q_OS_WIN32
3167
Returns a QString initialized with the first \a size characters
3168
of the 8-bit string \a str.
3170
If \a size is -1 (the default), it is taken to be qstrlen(\a
3173
QTextCodec::codecForLocale() is used to perform the conversion
3176
\sa toLocal8Bit(), fromAscii(), fromLatin1(), fromUtf8()
3178
QString QString::fromLocal8Bit(const char *str, int size)
3182
#if defined(Q_OS_WIN32)
3184
QByteArray ba(str, size); // creates a '\0'-terminated deep copy
3185
return qt_winMB2QString(ba, size);
3187
return qt_winMB2QString(str, size);
3189
#elif defined(Q_OS_UNIX)
3190
# if !defined(QT_NO_TEXTCODEC)
3192
size = qstrlen(str);
3193
QTextCodec *codec = QTextCodec::codecForLocale();
3195
return codec->toUnicode(str, size);
3196
# endif // !QT_NO_TEXTCODEC
3198
return fromLatin1(str, size);
3202
Returns a QString initialized with the first \a size characters
3203
of the 8-bit ASCII string \a str.
3205
If \a size is -1 (the default), it is taken to be qstrlen(\a
3208
If a codec has been set using QTextCodec::setCodecForCStrings(),
3209
it is used to convert \a str to Unicode; otherwise this function
3210
does the same as fromLatin1().
3212
\sa toAscii(), fromLatin1(), fromUtf8(), fromLocal8Bit()
3214
QString QString::fromAscii(const char *str, int size)
3216
#ifndef QT_NO_TEXTCODEC
3217
if (codecForCStrings) {
3220
if (size == 0 || (!*str && size < 0))
3221
return QLatin1String("");
3223
size = qstrlen(str);
3224
return codecForCStrings->toUnicode(str, size);
3227
return fromLatin1(str, size);
3230
static ushort *addOne(ushort *qch, QString &str)
3232
long sidx = qch - str.utf16();
3233
str.resize(str.length()+1);
3234
return (ushort *)str.data() + sidx;
3238
Returns a QString initialized with the first \a size bytes
3239
of the UTF-8 string \a str.
3241
If \a size is -1 (the default), it is taken to be qstrlen(\a
3244
\sa toUtf8(), fromAscii(), fromLatin1(), fromLocal8Bit()
3246
QString QString::fromUtf8(const char *str, int size)
3251
size = qstrlen(str);
3254
result.resize(size); // worst case
3255
ushort *qch = result.d->data;
3260
for (int i = 0; i < size; ++i) {
3263
if ((ch&0xc0) == 0x80) {
3264
uc = (uc << 6) | (ch & 0x3f);
3270
ushort high = uc/0x400 + 0xd800;
3271
ushort low = uc%0x400 + 0xdc00;
3279
// See QString::toUtf8() for explanation.
3281
// The surrogate below corresponds to a Unicode value of (0x10fe00+ch) which
3282
// is in one of the private use areas of Unicode.
3284
qch = addOne(qch, result);
3286
*qch++ = 0xde00 + ((uchar)str[i]);
3292
} else if ((ch & 0xe0) == 0xc0) {
3296
} else if ((ch & 0xf0) == 0xe0) {
3300
} else if ((ch&0xf8) == 0xf0) {
3306
qch = addOne(qch, result);
3308
*qch++ = 0xde00 + ((uchar)str[i]);
3313
// we have some invalid characters remaining we need to add to the string
3314
for (int i = error; i < size; ++i) {
3315
qch = addOne(qch, result);
3317
*qch++ = 0xde00 + (uchar)str[i];
3321
result.truncate(qch - result.d->data);
3326
Returns a QString initialized with the first \a size characters
3327
of the Unicode string \a unicode (ISO-10646-UTF-16 encoded).
3329
If \a size is -1 (the default), \a unicode must be terminated
3332
QString makes a deep copy of the Unicode data.
3334
\sa utf16(), setUtf16()
3336
QString QString::fromUtf16(const ushort *unicode, int size)
3342
while (unicode[size] != 0)
3345
return QString((const QChar *)unicode, size);
3349
Resizes the string to \a size characters and copies \a unicode
3352
If \a unicode is 0, nothing is copied, but the string is still
3355
\sa unicode(), setUtf16()
3357
QString& QString::setUnicode(const QChar *unicode, int size)
3360
if (unicode && size)
3361
memcpy(d->data, unicode, size * sizeof(QChar));
3366
\fn QString &QString::setUtf16(const ushort *unicode, int size)
3368
Resizes the string to \a size characters and copies \a unicode
3371
If \a unicode is 0, nothing is copied, but the string is still
3374
\sa utf16(), setUnicode()
3378
Returns a string that has whitespace removed from the start
3379
and the end, and that has each sequence of internal whitespace
3380
replaced with a single space.
3382
Whitespace means any character for which QChar::isSpace() returns
3383
true. This includes the ASCII characters '\\t', '\\n', '\\v',
3384
'\\f', '\\r', and ' '.
3388
QString str = " lots\t of\nwhitespace\r\n ";
3389
str = str.trimmed();
3390
// str == "lots of whitespace";
3395
QString QString::simplified() const
3400
result.resize(d->size);
3401
const QChar *from = (const QChar*) d->data;
3402
const QChar *fromend = (const QChar*) from+d->size;
3404
QChar *to = (QChar*) result.d->data;
3406
while (from!=fromend && from->isSpace())
3408
while (from!=fromend && !from->isSpace())
3409
to[outc++] = *from++;
3411
to[outc++] = QLatin1Char(' ');
3415
if (outc > 0 && to[outc-1] == QLatin1Char(' '))
3417
result.truncate(outc);
3422
Returns a string that has whitespace removed from the start and
3425
Whitespace means any character for which QChar::isSpace() returns
3426
true. This includes the ASCII characters '\\t', '\\n', '\\v',
3427
'\\f', '\\r', and ' '.
3431
QString str = " lots\t of\nwhitespace\r\n ";
3432
str = str.trimmed();
3433
// str == "lots\t of\nwhitespace";
3436
Unlike simplified(), trimmed() leaves internal whitespace alone.
3440
QString QString::trimmed() const
3444
const QChar *s = (const QChar*)d->data;
3445
if (!s->isSpace() && !s[d->size-1].isSpace())
3448
int end = d->size - 1;
3449
while (start<=end && s[start].isSpace()) // skip white space from start
3451
if (start <= end) { // only white space
3452
while (end && s[end].isSpace()) // skip white space from end
3455
int l = end - start + 1;
3457
shared_empty.ref.ref();
3458
return QString(&shared_empty, 0);
3460
return QString(s + start, l);
3463
/*! \fn const QChar QString::at(int i) const
3465
Returns the character at index position \a i in the string.
3467
\a i must be a valid index position in the string (i.e., 0 <= \a
3473
/*! \fn QCharRef QString::operator[](int i)
3475
Returns the character at index position \a i as a modifiable
3480
if (str[0] == QChar('?'))
3481
str[0] = QChar('_');
3484
The return value is of type QCharRef, a helper class for QString.
3485
When you get an object of type QCharRef, you can use it as if it
3486
were a QChar &. If you assign to it, the assignment will apply to
3487
the character in the QString from which you got the reference.
3492
/*! \fn const QChar QString::operator[](int i) const
3499
/*! \fn QCharRef QString::operator[](uint i)
3504
/*! \fn const QChar QString::operator[](uint i) const
3511
Truncates the string at index position \a pos.
3513
If \a pos is beyond the end of the string, nothing happens.
3517
QString str = "Vladivostok";
3522
\sa chop(), resize(), left()
3525
void QString::truncate(int pos)
3534
Removes \a n characters from the end of the string.
3536
If \a n is greater than size(), the result is an empty string.
3540
QString str("LOGOUT\r\n");
3545
If you want to remove characters from the \e beginning of the
3546
string, use remove() instead.
3548
\sa truncate(), resize(), remove()
3550
void QString::chop(int n)
3553
resize(d->size - n);
3557
Sets every character in the string to character \a ch. If \a size
3558
is different from -1 (the default), the string is resized to \a
3563
QString str = "Berlin";
3574
QString& QString::fill(QChar ch, int size)
3576
resize(size < 0 ? d->size : size);
3578
QChar *i = (QChar*)d->data + d->size;
3579
QChar *b = (QChar*)d->data;
3587
\fn int QString::length() const
3593
\fn int QString::size() const
3595
Returns the number of characters in this string.
3597
The last character in the string is at position size() - 1. In
3598
addition, QString ensures that the character at position size()
3599
is always '\\0', so that you can use the return value of data()
3600
and constData() as arguments to functions that expect
3601
'\\0'-terminated strings.
3605
QString str = "World";
3606
int n = str.size(); // n == 5
3607
str.data()[0]; // returns 'W'
3608
str.data()[4]; // returns 'd'
3609
str.data()[5]; // returns '\0'
3612
\sa isEmpty(), resize()
3615
/*! \fn bool QString::isNull() const
3617
Returns true if this string is null; otherwise returns false.
3621
QString().isNull(); // returns true
3622
QString("").isNull(); // returns false
3623
QString("abc").isNull(); // returns false
3626
Qt makes a distinction between null strings and empty strings for
3627
historical reasons. For most applications, what matters is
3628
whether or not a string contains any data, and this can be
3629
determined using isEmpty().
3634
/*! \fn bool QString::isEmpty() const
3636
Returns true if the string has no characters; otherwise returns
3641
QString().isEmpty(); // returns true
3642
QString("").isEmpty(); // returns true
3643
QString("x").isEmpty(); // returns false
3644
QString("abc").isEmpty(); // returns false
3650
/*! \fn QString &QString::operator+=(const QString &other)
3652
Appends the string \a other onto the end of this string and
3653
returns a reference to this string.
3663
This operation is typically very fast (\l{constant time}),
3664
because QString preallocates extra space at the end of the string
3665
data so it can grow without reallocating the entire string each
3668
\sa append(), prepend()
3671
/*! \fn QString &QString::operator+=(const QLatin1String &str)
3675
Appends the Latin-1 string \a str to this string.
3678
/*! \fn QString &QString::operator+=(const QByteArray &ba)
3682
Appends the byte array \a ba to this string. \a ba is converted
3683
to Unicode using fromAscii().
3685
You can disable this function by defining \c
3686
QT_NO_CAST_FROM_ASCII when you compile your applications. This
3687
can be useful if you want to ensure that all user-visible strings
3688
go through QObject::tr(), for example.
3691
/*! \fn QString &QString::operator+=(const char *str)
3695
Appends the string \a str to this string. \a str is converted to
3696
Unicode using fromAscii().
3698
You can disable this function by defining \c
3699
QT_NO_CAST_FROM_ASCII when you compile your applications. This
3700
can be useful if you want to ensure that all user-visible strings
3701
go through QObject::tr(), for example.
3704
/*! \fn QString &QString::operator+=(char ch)
3708
Appends the character \a ch to this string. The character is
3709
converted to Unicode using fromAscii().
3711
You can disable this function by defining \c
3712
QT_NO_CAST_FROM_ASCII when you compile your applications. This
3713
can be useful if you want to ensure that all user-visible strings
3714
go through QObject::tr(), for example.
3717
/*! \fn QString &QString::operator+=(QChar ch)
3721
Appends the character \a ch to the string.
3724
/*! \fn QString &QString::operator+=(QChar::SpecialCharacter c)
3732
\fn bool operator==(const char *s1, const QString &s2)
3737
Returns true if \a s1 is equal to \a s2; otherwise returns false.
3738
Note that no string is equal to \a s1 being 0.
3740
Equivalent to \a s1 != 0 && compare(\a s1, \a s2) == 0.
3744
\fn bool operator!=(const char *s1, const QString &s2)
3749
Returns true if \a s1 is not equal to \a s2; otherwise returns false.
3750
Note that no string is equal to \a s1 being 0.
3752
For \a s1 != 0, this is equivalent to compare(\a s1, \a s2) != 0.
3756
\fn bool operator<(const char *s1, const QString &s2)
3761
Returns true if \a s1 is lexically less than \a s2; otherwise returns false.
3763
The comparison is based exclusively on the numeric Unicode values
3764
of the characters and is very fast, but is not what a human would
3765
expect. Consider sorting user-interface strings with
3766
QString::localeAwareCompare().
3768
For \a s1 != 0, equivalent to compare(\a s1, \a s2) \< 0.
3772
\fn bool operator<=(const char *s1, const QString &s2)
3777
Returns true if \a s1 is lexically less than or equal to \a s2;
3778
otherwise returns false.
3780
The comparison is based exclusively on the numeric Unicode values
3781
of the characters and is very fast, but is not what a human would
3782
expect. Consider sorting user-interface strings with
3783
QString::localeAwareCompare().
3785
For \a s1 != 0, this is equivalent to compare(\a s1, \a s2) \<= 0.
3789
\fn bool operator>(const char *s1, const QString &s2)
3794
Returns true if \a s1 is lexically greater than \a s2; otherwise
3797
The comparison is based exclusively on the numeric Unicode values
3798
of the characters and is very fast, but is not what a human would
3799
expect. Consider sorting user-interface strings with
3800
QString::localeAwareCompare().
3802
Equivalent to compare(\a s1, \a s2) \> 0.
3806
\fn bool operator>=(const char *s1, const QString &s2)
3811
Returns true if \a s1 is lexically greater than or equal to \a s2;
3812
otherwise returns false.
3814
The comparison is based exclusively on the numeric Unicode values
3815
of the characters and is very fast, but is not what a human would
3816
expect. Consider sorting user-interface strings with
3817
QString::localeAwareCompare().
3819
For \a s1 != 0, this is equivalent to compare(\a s1, \a s2) \>= 0.
3823
\fn const QString operator+(const QString &s1, const QString &s2)
3827
Returns a string which is the result of concatenating the string
3828
\a s1 and the string \a s2.
3832
\fn const QString operator+(const QString &s1, const char *s2)
3837
Returns a string which is the result of concatenating the string
3838
\a s1 and the string \a s2. \a s2 is converted to
3839
Unicode using fromAscii().
3843
\fn const QString operator+(const char *s1, const QString &s2)
3848
Returns a string which is the result of concatenating the
3849
string \a s1 and string \a s2. \a s1 is converted to
3850
Unicode using fromAscii().
3854
\fn const QString operator+(const QString &s, char ch)
3859
Returns a string which is the result of concatenating the string
3860
\a s and character \a ch.
3864
\fn const QString operator+(char ch, const QString &s)
3869
Returns a string which is the result of concatenating the
3870
character \a ch and string \a s.
3874
\fn int QString::compare(const QString & s1, const QString & s2)
3876
Lexically compares \a s1 with \a s2 and returns an integer less
3877
than, equal to, or greater than zero if \a s1 is less than, equal
3878
to, or greater than \a s2.
3880
The comparison is based exclusively on the numeric Unicode values
3881
of the characters and is very fast, but is not what a human would
3882
expect. Consider sorting user-visible strings with
3883
localeAwareCompare().
3886
int x = QString::compare("auto", "auto"); // x == 0
3887
int y = QString::compare("auto", "car"); // y < 0
3888
int z = QString::compare("car", "auto"); // z > 0
3891
\sa localeAwareCompare(), operator==(), operator<(), operator>()
3897
Same as compare(*this, \a other).
3899
int QString::compare(const QString &other) const
3901
return ucstrcmp(*this, other);
3906
\fn int QString::localeAwareCompare(const QString & s1, const QString & s2)
3908
Compares \a s1 with \a s2 and returns an integer less than, equal
3909
to, or greater than zero if \a s1 is less than, equal to, or
3912
The comparison is performed in a locale- and also
3913
platform-dependent manner. Use this function to present sorted
3914
lists of strings to the user.
3916
\sa compare(), QTextCodec::locale()
3922
Same as localeAwareCompare(*this, \a other).
3925
#if !defined(CSTR_LESS_THAN)
3926
#define CSTR_LESS_THAN 1
3927
#define CSTR_EQUAL 2
3928
#define CSTR_GREATER_THAN 3
3931
int QString::localeAwareCompare(const QString &other) const
3933
// do the right thing for null and empty
3934
if (isEmpty() || other.isEmpty())
3935
return compare(other);
3937
#if defined(Q_OS_WIN32)
3940
const TCHAR* s1 = (TCHAR*)utf16();
3941
const TCHAR* s2 = (TCHAR*)other.utf16();
3942
res = CompareStringW(GetThreadLocale(), 0, s1, length(), s2, other.length());
3944
QByteArray s1 = toLocal8Bit();
3945
QByteArray s2 = other.toLocal8Bit();
3946
res = CompareStringA(GetThreadLocale(), 0, s1.data(), s1.length(), s2.data(), s2.length());
3950
case CSTR_LESS_THAN:
3952
case CSTR_GREATER_THAN:
3957
#elif defined(Q_OS_UNIX)
3958
// declared in <string.h>
3959
int delta = strcoll(toLocal8Bit(), other.toLocal8Bit());
3961
delta = ucstrcmp(*this, other);
3964
return ucstrcmp(*this, other);
3970
\fn const QChar *QString::unicode() const
3972
Returns a '\\0'-terminated Unicode representation of the string.
3973
The result remains valid until the string is modified.
3979
\fn const ushort *QString::utf16() const
3981
Returns the QString as a '\\0\'-terminated array of unsigned
3982
shorts. The result remains valid until the string is modified.
3987
const ushort *QString::utf16() const
3989
if (d->data != d->array) {
3990
QString *that = const_cast<QString*>(this);
3991
that->realloc(); // ensure '\\0'-termination for ::fromRawData strings
3992
return that->d->data;
3998
Returns a string of size() \a width that contains this string
3999
padded by the \a fill character.
4001
If \a truncate is false and the size() of the string is more than
4002
\a width, then the returned string is a copy of the string.
4004
If \a truncate is true and the size() of the string is more than
4005
\a width, then any characters in a copy of the string after
4006
position \a width are removed, and the copy is returned.
4009
QString s = "apple";
4010
QString t = s.leftJustified(8, '.'); // t == "apple..."
4013
\sa rightJustified()
4016
QString QString::leftJustified(int width, QChar fill, bool truncate) const
4020
int padlen = width - len;
4022
result.resize(len+padlen);
4024
memcpy(result.d->data, d->data, sizeof(QChar)*len);
4025
QChar *uc = (QChar*)result.d->data + len;
4030
result = left(width);
4038
Returns a string of size() \a width that contains the \a fill
4039
character followed by the string.
4041
If \a truncate is false and the size() of the string is more than
4042
\a width, then the returned string is a copy of the string.
4044
If \a truncate is true and the size() of the string is more than
4045
\a width, then the resulting string is truncated at position \a
4049
QString str = "apple";
4050
str = str.rightJustified(8, '.');
4051
// str == "...apple"
4057
QString QString::rightJustified(int width, QChar fill, bool truncate) const
4061
int padlen = width - len;
4063
result.resize(len+padlen);
4064
QChar *uc = (QChar*)result.d->data;
4068
memcpy(uc, d->data, sizeof(QChar)*len);
4071
result = left(width);
4079
Returns a lowercase copy of the string.
4082
QString str = "TROlltECH";
4083
str = str.toLower(); // str == "trolltech"
4089
QString QString::toLower() const
4093
QChar *p = (QChar*)d->data;
4097
if (p->unicode() & 0xFF80)
4098
different = (*p != ::lower(*p));
4100
different = ((uint)p->cell() - 'A' < 26);
4104
p = (QChar*)s.data() + (p - (QChar*)d->data);
4121
Returns an uppercase copy of the string.
4124
QString str = "TeXt";
4125
str = str.toUpper(); // str == "TEXT"
4131
QString QString::toUpper() const
4135
QChar *p = (QChar*)d->data;
4139
if (p->unicode() & 0xFF80)
4140
different = (*p != ::upper(*p));
4142
different = ((uint)p->cell() - 'a' < 26);
4146
p = s.data() + (p - (QChar*)d->data);
4163
#ifndef QT_NO_SPRINTF
4165
Safely builds a formatted string from the format string \a cformat
4166
and an arbitrary list of arguments.
4168
The %lc escape sequence expects a unicode character of type ushort
4169
(as returned by QChar::unicode()). The %ls escape sequence expects
4170
a pointer to a zero-terminated array of unicode characters of type
4171
ushort (as returned by QString::utf16()).
4173
The format string supports most of the conversion specifiers
4174
provided by printf() in the standard C++ library. It doesn't
4175
honor the length modifiers (e.g. \c h for \c short, \c ll for
4176
\c{long long}). If you need those, use the standard sprintf()
4181
::sprintf(buf, "%lld", 123456789LL);
4182
QString str = QString::fromAscii(buf);
4185
\warning We do not recommend using QString::sprintf() in new Qt
4186
code. Instead, consider using QTextOStream or arg(), both of
4187
which support Unicode strings seamlessly and are type-safe.
4188
Here's an example that uses QTextOStream:
4192
QTextOStream(&result) << "pi = " << 3.14;
4193
// result == "pi = 3.14"
4196
For \link QObject::tr() translations,\endlink especially if the
4197
strings contains more than one escape sequence, you should
4198
consider using the arg() function instead. This allows the order
4199
of the replacements to be controlled by the translator.
4204
QString &QString::sprintf(const char *cformat, ...)
4207
va_start(ap, cformat);
4208
QString &s = vsprintf(cformat, ap);
4214
Equivalent method to sprintf(), but takes a va_list \a ap
4215
instead a list of variable arguments. See the sprintf()
4216
documentation for an explanation of \a cformat.
4218
This method does not call the va_end macro, the caller
4219
is responsible to call va_end on \a ap.
4224
QString &QString::vsprintf(const char* cformat, va_list ap)
4226
QLocale locale(QLocale::C);
4228
if (!cformat || !*cformat) {
4230
*this = fromLatin1("");
4237
const char *c = cformat;
4239
// Copy non-escape chars to result
4240
while (*c != '\0' && *c != '%')
4241
result.append(QLatin1Char(*c++));
4247
const char *escape_start = c;
4251
result.append(QLatin1Char('%')); // a % at the end of the string - treat as non-escape text
4255
result.append(QLatin1Char('%')); // %%
4260
// Parse flag characters
4262
bool no_more_flags = false;
4265
case '#': flags |= QLocalePrivate::Alternate; break;
4266
case '0': flags |= QLocalePrivate::ZeroPadded; break;
4267
case '-': flags |= QLocalePrivate::LeftAdjusted; break;
4268
case ' ': flags |= QLocalePrivate::BlankBeforePositive; break;
4269
case '+': flags |= QLocalePrivate::AlwaysShowSign; break;
4270
case '\'': flags |= QLocalePrivate::ThousandsGroup; break;
4271
default: no_more_flags = true; break;
4276
} while (!no_more_flags);
4279
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
4283
// Parse field width
4284
int width = -1; // -1 means unspecified
4287
while (*c != '\0' && qIsDigit(*c))
4288
width_str.append(QLatin1Char(*c++));
4290
// can't be negative - started with a digit
4291
// contains at least one digit
4292
width = width_str.toInt();
4294
else if (*c == '*') {
4295
width = va_arg(ap, int);
4297
width = -1; // treat all negative numbers as unspecified
4302
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
4307
int precision = -1; // -1 means unspecified
4311
QString precision_str;
4312
while (*c != '\0' && qIsDigit(*c))
4313
precision_str.append(QLatin1Char(*c++));
4315
// can't be negative - started with a digit
4316
// contains at least one digit
4317
precision = precision_str.toInt();
4319
else if (*c == '*') {
4320
precision = va_arg(ap, int);
4322
precision = -1; // treat all negative numbers as unspecified
4328
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
4332
// Parse the length modifier
4333
enum LengthMod { lm_none, lm_hh, lm_h, lm_l, lm_ll, lm_L, lm_j, lm_z, lm_t };
4334
LengthMod length_mod = lm_none;
4381
result.append(QLatin1String(escape_start)); // incomplete escape, treat as non-escape text
4385
// Parse the conversion specifier and do the conversion
4391
switch (length_mod) {
4392
case lm_none: i = va_arg(ap, int); break;
4393
case lm_hh: i = va_arg(ap, int); break;
4394
case lm_h: i = va_arg(ap, int); break;
4395
case lm_l: i = va_arg(ap, long int); break;
4396
case lm_ll: i = va_arg(ap, qint64); break;
4397
case lm_j: i = va_arg(ap, long int); break;
4398
case lm_z: i = va_arg(ap, size_t); break;
4399
case lm_t: i = va_arg(ap, int); break;
4400
default: i = 0; break;
4402
subst = locale.d->longLongToString(i, precision, 10, width, flags);
4411
switch (length_mod) {
4412
case lm_none: u = va_arg(ap, uint); break;
4413
case lm_hh: u = va_arg(ap, uint); break;
4414
case lm_h: u = va_arg(ap, uint); break;
4415
case lm_l: u = va_arg(ap, ulong); break;
4416
case lm_ll: u = va_arg(ap, quint64); break;
4417
default: u = 0; break;
4421
flags |= QLocalePrivate::CapitalEorX;
4424
switch (qToLower(*c)) {
4433
subst = locale.d->unsLongLongToString(u, precision, base, width, flags);
4446
if (length_mod == lm_L)
4447
d = va_arg(ap, long double); // not supported - converted to a double
4449
d = va_arg(ap, double);
4452
flags |= QLocalePrivate::CapitalEorX;
4454
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
4455
switch (qToLower(*c)) {
4456
case 'e': form = QLocalePrivate::DFExponent; break;
4457
case 'a': // not supported - decimal form used instead
4458
case 'f': form = QLocalePrivate::DFDecimal; break;
4459
case 'g': form = QLocalePrivate::DFSignificantDigits; break;
4462
subst = locale.d->doubleToString(d, precision, form, width, flags);
4467
if (length_mod == lm_l)
4468
subst = QChar((ushort) va_arg(ap, int));
4470
subst = QLatin1Char((uchar) va_arg(ap, int));
4475
if (length_mod == lm_l) {
4476
const ushort *buff = va_arg(ap, const ushort*);
4477
const ushort *ch = buff;
4480
subst.setUtf16(buff, ch - buff);
4482
subst = QString::fromUtf8(va_arg(ap, const char*));
4483
if (precision != -1)
4484
subst.truncate(precision);
4489
void *arg = va_arg(ap, void*);
4491
quint64 i = reinterpret_cast<quint64>(arg);
4493
quint64 i = reinterpret_cast<unsigned long>(arg);
4495
flags |= QLocalePrivate::Alternate;
4496
subst = locale.d->unsLongLongToString(i, precision, 16, width, flags);
4501
switch (length_mod) {
4503
signed char *n = va_arg(ap, signed char*);
4504
*n = result.length();
4508
short int *n = va_arg(ap, short int*);
4509
*n = result.length();
4513
long int *n = va_arg(ap, long int*);
4514
*n = result.length();
4518
qint64 *n = va_arg(ap, qint64*);
4519
volatile uint tmp = result.length(); // egcs-2.91.66 gets internal
4520
*n = tmp; // compiler error without volatile
4524
int *n = va_arg(ap, int*);
4525
*n = result.length();
4532
default: // bad escape, treat as non-escape text
4533
for (const char *cc = escape_start; cc != c; ++cc)
4534
result.append(QLatin1Char(*cc));
4538
if (flags & QLocalePrivate::LeftAdjusted)
4539
result.append(subst.leftJustified(width));
4541
result.append(subst.rightJustified(width));
4551
Returns the string converted to a \c{long long} using base \a
4552
base, which is 10 by default and must be between 2 and 36, or 0.
4553
Returns 0 if the conversion fails.
4555
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4556
false; otherwise *\a{ok} is set to true.
4558
If \a base is 0, the C language convention is used: If the string
4559
begins with "0x", base 16 is used; if the string begins with "0",
4560
base 8 is used; otherwise, base 10 is used.
4567
qint64 hex = str.toLongLong(&ok, 16); // hex == 255, ok == true
4568
qint64 dec = str.toLongLong(&ok, 10); // dec == 0, ok == false
4571
\sa number(), toULongLong(), toInt()
4574
qint64 QString::toLongLong(bool *ok, int base) const
4576
#if defined(QT_CHECK_RANGE)
4577
if (base != 0 && (base < 2 || base > 36)) {
4578
qWarning("QString::toLongLong: Invalid base (%d)", base);
4585
qint64 result = def_locale.d->stringToLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
4592
// If the default was not "C", try the "C" locale
4593
if (def_locale.language() == QLocale::C) {
4599
QLocale c_locale(QLocale::C);
4600
return c_locale.d->stringToLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
4604
Returns the string converted to an \c{unsigned long long} using base \a
4605
base, which is 10 by default and must be between 2 and 36, or 0.
4606
Returns 0 if the conversion fails.
4608
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4609
false; otherwise *\a{ok} is set to true.
4611
If \a base is 0, the C language convention is used: If the string
4612
begins with "0x", base 16 is used; if the string begins with "0",
4613
base 8 is used; otherwise, base 10 is used.
4620
quint64 hex = str.toULongLong(&ok, 16); // hex == 255, ok == true
4621
quint64 dec = str.toULongLong(&ok, 10); // dec == 0, ok == false
4624
\sa number(), toLongLong()
4627
quint64 QString::toULongLong(bool *ok, int base) const
4629
#if defined(QT_CHECK_RANGE)
4630
if (base != 0 && (base < 2 || base > 36)) {
4631
qWarning("QString::toULongLong: Invalid base %d", base);
4638
quint64 result = def_locale.d->stringToUnsLongLong(*this, base, &my_ok, QLocalePrivate::FailOnGroupSeparators);
4645
// If the default was not "C", try the "C" locale
4646
if (def_locale.language() == QLocale::C) {
4652
QLocale c_locale(QLocale::C);
4653
return c_locale.d->stringToUnsLongLong(*this, base, ok, QLocalePrivate::FailOnGroupSeparators);
4657
\fn long QString::toLong(bool *ok, int base) const
4659
Returns the string converted to a \c long using base \a
4660
base, which is 10 by default and must be between 2 and 36, or 0.
4661
Returns 0 if the conversion fails.
4663
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4664
false; otherwise *\a{ok} is set to true.
4666
If \a base is 0, the C language convention is used: If the string
4667
begins with "0x", base 16 is used; if the string begins with "0",
4668
base 8 is used; otherwise, base 10 is used.
4675
long hex = str.toLong(&ok, 16); // hex == 255, ok == true
4676
long dec = str.toLong(&ok, 10); // dec == 0, ok == false
4679
\sa number(), toULong(), toInt()
4682
long QString::toLong(bool *ok, int base) const
4684
qint64 v = toLongLong(ok, base);
4685
if (v < LONG_MIN || v > LONG_MAX) {
4694
\fn ulong QString::toULong(bool *ok, int base) const
4696
Returns the string converted to an \c{unsigned long} using base \a
4697
base, which is 10 by default and must be between 2 and 36, or 0.
4698
Returns 0 if the conversion fails.
4700
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4701
false; otherwise *\a{ok} is set to true.
4703
If \a base is 0, the C language convention is used: If the string
4704
begins with "0x", base 16 is used; if the string begins with "0",
4705
base 8 is used; otherwise, base 10 is used.
4712
ulong hex = str.toULong(&ok, 16); // hex == 255, ok == true
4713
ulong dec = str.toULong(&ok, 10); // dec == 0, ok == false
4719
ulong QString::toULong(bool *ok, int base) const
4721
quint64 v = toULongLong(ok, base);
4722
if (v > ULONG_MAX) {
4732
Returns the string converted to an \c int using base \a
4733
base, which is 10 by default and must be between 2 and 36, or 0.
4734
Returns 0 if the conversion fails.
4736
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4737
false; otherwise *\a{ok} is set to true.
4739
If \a base is 0, the C language convention is used: If the string
4740
begins with "0x", base 16 is used; if the string begins with "0",
4741
base 8 is used; otherwise, base 10 is used.
4748
int hex = str.toInt(&ok, 16); // hex == 255, ok == true
4749
int dec = str.toInt(&ok, 10); // dec == 0, ok == false
4752
\sa number(), toUInt(), toDouble()
4755
int QString::toInt(bool *ok, int base) const
4757
qint64 v = toLongLong(ok, base);
4758
if (v < INT_MIN || v > INT_MAX) {
4767
Returns the string converted to an \c{unsigned int} using base \a
4768
base, which is 10 by default and must be between 2 and 36, or 0.
4769
Returns 0 if the conversion fails.
4771
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4772
false; otherwise *\a{ok} is set to true.
4774
If \a base is 0, the C language convention is used: If the string
4775
begins with "0x", base 16 is used; if the string begins with "0",
4776
base 8 is used; otherwise, base 10 is used.
4783
uint hex = str.toUInt(&ok, 16); // hex == 255, ok == true
4784
uint dec = str.toUInt(&ok, 10); // dec == 0, ok == false
4787
\sa number(), toInt()
4790
uint QString::toUInt(bool *ok, int base) const
4792
quint64 v = toULongLong(ok, base);
4802
Returns the string converted to a \c short using base \a
4803
base, which is 10 by default and must be between 2 and 36, or 0.
4804
Returns 0 if the conversion fails.
4806
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4807
false; otherwise *\a{ok} is set to true.
4809
If \a base is 0, the C language convention is used: If the string
4810
begins with "0x", base 16 is used; if the string begins with "0",
4811
base 8 is used; otherwise, base 10 is used.
4818
short hex = str.toShort(&ok, 16); // hex == 255, ok == true
4819
short dec = str.toShort(&ok, 10); // dec == 0, ok == false
4822
\sa number(), toUShort(), toInt()
4825
short QString::toShort(bool *ok, int base) const
4827
long v = toLongLong(ok, base);
4828
if (v < SHRT_MIN || v > SHRT_MAX) {
4837
Returns the string converted to an \c{unsigned short} using base \a
4838
base, which is 10 by default and must be between 2 and 36, or 0.
4839
Returns 0 if the conversion fails.
4841
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4842
false; otherwise *\a{ok} is set to true.
4844
If \a base is 0, the C language convention is used: If the string
4845
begins with "0x", base 16 is used; if the string begins with "0",
4846
base 8 is used; otherwise, base 10 is used.
4853
ushort hex = str.toUShort(&ok, 16); // hex == 255, ok == true
4854
ushort dec = str.toUShort(&ok, 10); // dec == 0, ok == false
4857
\sa number(), toShort()
4860
ushort QString::toUShort(bool *ok, int base) const
4862
ulong v = toULongLong(ok, base);
4863
if (v > USHRT_MAX) {
4873
Returns the string converted to a \c double value.
4875
Returns 0.0 if the conversion fails.
4877
If \a ok is not 0: if a conversion error occurs, \c{*}\a{ok} is set to
4878
false; otherwise \c{*}\a{ok} is set to true.
4881
QString str = "1234.56";
4882
double val = str.toDouble(); // val == 1234.56
4885
This function tries to interpret the string according to the
4886
current locale. The current locale is determined from the
4887
system at application startup and can be changed by calling
4888
QLocale::setDefault(). If the string cannot be interpreted
4889
according to the current locale, this function falls back
4896
QLocale::setDefault(QLocale::C);
4897
d = QString( "1234,56" ).toDouble(&ok); // ok == false
4898
d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
4900
QLocale::setDefault(QLocale::German);
4901
d = QString( "1234,56" ).toDouble(&ok); // ok == true, d == 1234.56
4902
d = QString( "1234.56" ).toDouble(&ok); // ok == true, d == 1234.56
4905
Due to the ambiguity between the decimal point and thousands group
4906
separator in various locales, this function does not handle
4907
thousands group separators. If you need to convert such numbers,
4908
see QLocale::toDouble().
4912
QLocale::setDefault(QLocale::C);
4913
double d = QString( "1,234,567.89" ).toDouble(&ok); // ok == false
4916
\warning If the string contains trailing whitespace this function
4917
will fail, and set \c{*}\a{ok} to false if \a ok is not 0. Leading
4918
whitespace is ignored.
4920
\sa number() QLocale::setDefault() QLocale::toDouble() trimmed()
4923
double QString::toDouble(bool *ok) const
4927
double result = def_locale.d->stringToDouble(*this, &my_ok, QLocalePrivate::FailOnGroupSeparators);
4934
// If the default was not "C", try the "C" locale
4935
if (def_locale.language() == QLocale::C) {
4941
QLocale c_locale(QLocale::C);
4942
return c_locale.d->stringToDouble(*this, ok, QLocalePrivate::FailOnGroupSeparators);
4946
Returns the string converted to a \c float value.
4948
Returns 0.0 if the conversion fails.
4950
If \a ok is not 0: if a conversion error occurs, *\a{ok} is set to
4951
false; otherwise *\a{ok} is set to true.
4955
QString str1 = "1234.56";
4956
str1.toFloat(); // returns 1234.56
4959
QString str2 = "R2D2";
4960
str2.toFloat(&ok); // returns 0.0, sets ok to false
4963
\sa number(), toDouble(), toInt()
4966
#define QT_MAX_FLOAT 3.4028234663852886e+38
4968
float QString::toFloat(bool *ok) const
4971
double d = toDouble(&myOk);
4972
if (!myOk || d > QT_MAX_FLOAT || d < -QT_MAX_FLOAT) {
4982
/*! \fn QString &QString::setNum(int n, int base)
4984
Sets the string to the printed value of \a n in base \a base and
4985
returns a reference to the string.
4987
The base is 10 by default and must be between 2 and 36.
4991
str.setNum(1234); // str == "1234"
4995
/*! \fn QString &QString::setNum(uint n, int base)
5000
/*! \fn QString &QString::setNum(long n, int base)
5005
/*! \fn QString &QString::setNum(ulong n, int base)
5013
QString &QString::setNum(qlonglong n, int base)
5015
#if defined(QT_CHECK_RANGE)
5016
if (base < 2 || base > 36) {
5017
qWarning("QString::setNum: Invalid base %d", base);
5021
QLocale locale(QLocale::C);
5022
*this = locale.d->longLongToString(n, -1, base);
5029
QString &QString::setNum(qulonglong n, int base)
5031
#if defined(QT_CHECK_RANGE)
5032
if (base < 2 || base > 36) {
5033
qWarning("QString::setNum: Invalid base %d", base);
5037
QLocale locale(QLocale::C);
5038
*this = locale.d->unsLongLongToString(n, -1, base);
5042
/*! \fn QString &QString::setNum(short n, int base)
5047
/*! \fn QString &QString::setNum(ushort n, int base)
5055
Sets the string to the printed value of \a n, formatted using
5056
format \a f with precision \a prec, and returns a reference to the
5059
The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'.
5060
See \l{#arg-formats}{arg()} for an explanation of the formats.
5063
QString &QString::setNum(double n, char f, int prec)
5065
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
5069
flags = QLocalePrivate::CapitalEorX;
5074
form = QLocalePrivate::DFDecimal;
5077
form = QLocalePrivate::DFExponent;
5080
form = QLocalePrivate::DFSignificantDigits;
5083
#if defined(QT_CHECK_RANGE)
5084
qWarning("QString::setNum: Invalid format char '%c'", f);
5089
QLocale locale(QLocale::C);
5090
*this = locale.d->doubleToString(n, prec, form, -1, flags);
5095
\fn QString &QString::setNum(float n, char f, int prec)
5099
Sets the string to the printed value of \a n, formatted in format
5100
\a f with precision \a prec, and returns a reference to the
5103
The format \a f can be 'f', 'F', 'e', 'E', 'g' or 'G'.
5104
See \l{#arg-formats}{arg()} for an explanation of the formats.
5109
\fn QString QString::number(long n, int base)
5111
Returns a string equivalent of the number \a n to base \a base,
5112
which is 10 by default and must be between 2 and 36.
5116
QString str = QString::number(a, 16); // str == "3f"
5117
QString str = QString::number(a, 16).upper(); // str == "3F"
5123
QString QString::number(long n, int base)
5131
\fn QString QString::number(ulong n, int base)
5135
QString QString::number(ulong n, int base)
5145
QString QString::number(int n, int base)
5155
QString QString::number(uint n, int base)
5165
QString QString::number(qlonglong n, int base)
5175
QString QString::number(qulonglong n, int base)
5186
Argument \a n is formatted according to the format \a f, and the
5187
precision \a prec. The format \a f can be 'f', 'F', 'e', 'E', 'g'
5188
or 'G'. See \l{#arg-formats}{arg()} for an explanation of the
5193
QString QString::number(double n, char f, int prec)
5196
s.setNum(n, f, prec);
5201
Splits the string into substrings wherever \a sep occurs, and
5202
returns the list of those strings. If \a sep does not match
5203
anywhere in the string, split() returns a single-element list
5204
containing this string.
5206
If \a cs is true, the string is only split only where characters
5207
are found that match \a sep exactly. If \a cs is false, the
5208
string is split, the string is split where characters are found
5209
that match \a sep case insensitively (e.g. "and" matches "AND").
5211
If \a behavior is QString::SkipEmptyParts, empty entries don't
5212
appear in the result. By default, empty entries are kept.
5216
QString str = "a,,b,c";
5217
QStringList list1 = str.split(",");
5218
// list1: [ "a", "", "b", "c" ]
5220
QStringList list2 = str.split(",", QString::SkipEmptyParts);
5221
// list2: [ "a", "b", "c" ]
5224
\sa QStringList::join(), section()
5226
QStringList QString::split(const QString &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
5232
while ((end = indexOf(sep, start + extra, cs)) != -1) {
5233
if (start != end || behavior == KeepEmptyParts)
5234
list.append(mid(start, end - start));
5235
start = end + sep.size();
5236
extra = (sep.size() == 0 ? 1 : 0);
5238
if (start != size() || behavior == KeepEmptyParts)
5239
list.append(mid(start));
5246
QStringList QString::split(const QChar &sep, SplitBehavior behavior, Qt::CaseSensitivity cs) const
5251
while ((end = indexOf(sep, start, cs)) != -1) {
5252
if (start != end || behavior == KeepEmptyParts)
5253
list.append(mid(start, end - start));
5256
if (start != size() || behavior == KeepEmptyParts)
5257
list.append(mid(start));
5261
#ifndef QT_NO_REGEXP
5265
Splits the string into substrings wherever the regular expression
5266
\a rx matches, and returns the list of those strings. If \a rx
5267
does not match anywhere in the string, split() returns a
5268
single-element list containing this string.
5270
Here's an example where we extract the words in a sentence
5271
using one or more whitespace characters as the separator:
5274
QString str = "Some text\n\twith strange whitespace.";
5275
QStringList list = str.split(QRegExp("\\s+"));
5276
// list: [ "Some", "text", "with", "strange", "whitespace." ]
5279
Here's a similar example, but this time we use any sequence of
5280
non-word characters as the separator:
5283
QString str = "This time, a normal English sentence.";
5284
QStringList list = str.split(QRegExp("\\W+"),
5285
QString::SkipEmptyParts);
5286
// list: [ "This", "time", "a", "normal", "English", "sentence" ]
5289
Here's a third example where we use a zero-length assertion,
5290
\bold{\\b} (word boundary), to split the string into an
5291
alternating sequence of non-word and word tokens:
5294
QString str = "Now: this sentence fragment.";
5295
QStringList list = str.split(QRegExp("\\b"));
5296
// list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ]
5299
\sa QStringList::join(), section()
5301
QStringList QString::split(const QRegExp &rx, SplitBehavior behavior) const
5307
while ((end = indexOf(rx, start + extra)) != -1) {
5308
int matchedLen = rx.matchedLength();
5309
if (start != end || behavior == KeepEmptyParts)
5310
list.append(mid(start, end - start));
5311
start = end + matchedLen;
5312
extra = (matchedLen == 0) ? 1 : 0;
5314
if (start != size() || behavior == KeepEmptyParts)
5315
list.append(mid(start));
5321
\enum QString::NormalizationForm
5323
\value NormalizationForm_D Canonical Decomposition
5324
\value NormalizationForm_C Canonical Decomposition followed by Canonical Composition
5325
\value NormalizationForm_KD Compatibility Decomposition
5326
\value NormalizationForm_KC Compatibility Decomposition followed by Canonical Composition
5329
{http://www.unicode.org/reports/tr15/}{Unicode Standard Annex #15}
5333
Returns the string in the given Unicode normalization \a form.
5335
QString QString::normalized(NormalizationForm form) const
5337
return QUnicodeTables::normalize(*this, form);
5343
Returns the string in the given Unicode normalization \a form,
5344
according to the given \a version of the Unicode standard.
5346
QString QString::normalized(NormalizationForm form, QChar::UnicodeVersion version) const
5348
return QUnicodeTables::normalize(*this, form, version);
5351
struct ArgEscapeData
5353
int min_escape; // lowest escape sequence number
5354
int occurrences; // number of occurrences of the lowest escape sequence number
5355
int locale_occurrences; // number of occurrences of the lowest escape sequence number that
5357
int escape_len; // total length of escape sequences which will be replaced
5360
static ArgEscapeData findArgEscapes(const QString &s)
5362
const QChar *uc_begin = s.unicode();
5363
const QChar *uc_end = uc_begin + s.length();
5367
d.min_escape = INT_MAX;
5370
d.locale_occurrences = 0;
5372
const QChar *c = uc_begin;
5373
while (c != uc_end) {
5374
while (c != uc_end && c->unicode() != '%')
5379
const QChar *escape_start = c;
5383
bool locale_arg = false;
5384
if (c->unicode() == 'L') {
5390
if (c->digitValue() == -1)
5393
int escape = c->digitValue();
5396
if (c != uc_end && c->digitValue() != -1) {
5397
escape = (10 * escape) + c->digitValue();
5401
if (escape > d.min_escape)
5404
if (escape < d.min_escape) {
5405
d.min_escape = escape;
5408
d.locale_occurrences = 0;
5413
++d.locale_occurrences;
5414
d.escape_len += c - escape_start;
5419
static QString replaceArgEscapes(const QString &s, const ArgEscapeData &d, int field_width,
5420
const QString &arg, const QString &larg, const QChar &fillChar = QLatin1Char(' '))
5422
const QChar *uc_begin = s.unicode();
5423
const QChar *uc_end = uc_begin + s.length();
5425
int abs_field_width = qAbs(field_width);
5426
int result_len = s.length()
5428
+ (d.occurrences - d.locale_occurrences)
5429
*qMax(abs_field_width, arg.length())
5430
+ d.locale_occurrences
5431
*qMax(abs_field_width, larg.length());
5434
result.resize(result_len);
5435
QChar *result_buff = (QChar*) result.unicode();
5437
QChar *rc = result_buff;
5438
const QChar *c = uc_begin;
5440
while (c != uc_end) {
5441
/* We don't have to check if we run off the end of the string with c,
5442
because as long as d.occurrences > 0 we KNOW there are valid escape
5445
const QChar *text_start = c;
5447
while (c->unicode() != '%')
5450
const QChar *escape_start = c++;
5452
bool locale_arg = false;
5453
if (c->unicode() == 'L') {
5458
int escape = c->digitValue();
5460
if (c + 1 != uc_end && (c + 1)->digitValue() != -1) {
5461
escape = (10 * escape) + (c + 1)->digitValue();
5466
if (escape != d.min_escape) {
5467
memcpy(rc, text_start, (c - text_start)*sizeof(QChar));
5468
rc += c - text_start;
5473
memcpy(rc, text_start, (escape_start - text_start)*sizeof(QChar));
5474
rc += escape_start - text_start;
5478
pad_chars = qMax(abs_field_width, larg.length()) - larg.length();
5480
pad_chars = qMax(abs_field_width, arg.length()) - arg.length();
5482
if (field_width > 0) { // left padded
5483
for (uint i = 0; i < pad_chars; ++i)
5484
(rc++)->unicode() = fillChar.unicode();
5488
memcpy(rc, larg.unicode(), larg.length()*sizeof(QChar));
5489
rc += larg.length();
5492
memcpy(rc, arg.unicode(), arg.length()*sizeof(QChar));
5496
if (field_width < 0) { // right padded
5497
for (uint i = 0; i < pad_chars; ++i)
5498
(rc++)->unicode() = fillChar.unicode();
5501
if (++repl_cnt == d.occurrences) {
5502
memcpy(rc, c, (uc_end - c)*sizeof(QChar));
5504
Q_ASSERT(rc - result_buff == result_len);
5509
Q_ASSERT(rc == result_buff + result_len);
5515
This function returns a copy of this string where \a a replaces
5516
the lowest numbered occurrence of \c %1, \c %2, ..., \c %9.
5518
The \a fieldWidth value specifies the minimum amount of space that
5519
\a a is padded to and filled with the character \a fillChar. A
5520
positive value will produce right-aligned text, whereas a negative
5521
value will produce left-aligned text.
5523
The following example shows how we could create a 'status' string
5524
when processing a list of files:
5527
QString status = QString("Processing file %1 of %2: %3")
5528
.arg(i) // current file's number
5529
.arg(total) // number of files to process
5530
.arg(fileName); // current file's name
5533
It is generally fine to use file names and numbers as we have
5534
done in the example above. But note that using arg() to construct
5535
natural language sentences does not usually translate well into
5536
other languages because sentence structure and word order often
5537
differ between languages.
5539
If there is no place marker (\c %1, \c %2, etc.), a warning
5540
message is output and the result is undefined.
5542
QString QString::arg(const QString &a, int fieldWidth, const QChar &fillChar) const
5544
ArgEscapeData d = findArgEscapes(*this);
5546
if (d.occurrences == 0) {
5547
qWarning("QString::arg(): Argument missing: %s, %s", toLocal8Bit().data(),
5548
a.toLocal8Bit().data());
5551
return replaceArgEscapes(*this, d, fieldWidth, a, a, fillChar);
5555
\fn QString QString::arg(const QString& a1, const QString& a2) const
5558
This is the same as str.arg(\a a1).arg(\a a2), except that
5559
the strings are replaced in one pass. This can make a difference
5560
if \a a1 contains e.g. \c{%1}:
5563
QString str = "%1 %2";
5564
str.arg("%1f", "Hello"); // returns "%1f Hello"
5565
str.arg("%1f").arg("Hello"); // returns "Hellof"
5570
\fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3) const
5573
This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a a3),
5574
except that the strings are replaced in one pass.
5578
\fn QString QString::arg(const QString& a1, const QString& a2, const QString& a3, const QString& a4) const
5581
This is the same as calling str.arg(\a a1).arg(\a a2).arg(\a
5582
a3).arg(\a a4), except that the strings are replaced in one pass.
5585
/*! \fn QString QString::arg(int a, int fieldWidth, int base, const QChar &fillChar) const
5589
\a a is expressed in base \a base, which is 10 by default and must
5590
be between 2 and 36.
5592
The \a fieldWidth value specifies the minimum amount of space that
5593
\a a is padded to and filled with the character \a fillChar. A
5594
positive value will produce a right-aligned number, whereas a
5595
negative value will produce a left-aligned number.
5597
The '%' can be followed by an 'L', in which case the sequence is
5598
replaced with a localized representation of \a a. The conversion uses
5599
the default locale, set by QLocale::setDefaultLocale(). If no default
5600
locale was specified, the "C" locale is used. The 'L' flag is ignored
5601
if \a base is not 10.
5605
str = QString("Decimal 63 is %1 in hexadecimal")
5607
// str == "Decimal 63 is 3f in hexadecimal"
5609
QLocale::setDefaultLocale(QLocale::English, QLocale::UnitedStates);
5610
str = QString("%1 %L2 %L3")
5614
// str == "12345 12,345 3039"
5618
/*! \fn QString QString::arg(uint a, int fieldWidth, int base, const QChar &fillChar) const
5622
\a base is the base to use when converting the integer \a a into a
5623
string. \a base must be between 2 and 36, with 8 giving octal, 10
5624
decimal, and 16 hexadecimal numbers.
5627
/*! \fn QString QString::arg(long a, int fieldWidth, int base, const QChar &fillChar) const
5631
The \a fieldWidth value specifies the minimum amount of space that
5632
\a a is padded to and filled with the character \a fillChar. A
5633
positive value will produce a right-aligned number, whereas a
5634
negative value will produce a left-aligned number.
5636
\a a is expressed in base \a base, which is 10 by default and must
5637
be between 2 and 36.
5639
The '%' can be followed by an 'L', in which case the sequence is
5640
replaced with a localized representation of \a a. The conversion
5641
uses the default locale. The default locale is determined from the
5642
system's locale settings at application startup. It can be changed
5643
using QLocale::setDefault(). The 'L' flag is ignored if \a base is
5648
str = QString( "Decimal 63 is %1 in hexadecimal" )
5650
// str == "Decimal 63 is 3f in hexadecimal"
5652
QLocale::setDefault(QLocale::English, QLocale::UnitedStates);
5653
str = QString( "%1 %L2 %L3" )
5656
.arg( 12345, 0, 16 );
5657
// str == "12345 12,345 3039"
5661
/*! \fn QString QString::arg(ulong a, int fieldWidth, int base, const QChar &fillChar) const
5665
\a base is the base to use when converting the integer \a a into a
5666
string. \a base must be between 2 and 36, with 8 giving octal, 10
5667
decimal, and 16 hexadecimal numbers.
5673
\a base is the base to use when converting the integer \a a into a
5674
string. \a base must be between 2 and 36, with 8 giving octal, 10
5675
decimal, and 16 hexadecimal numbers.
5677
QString QString::arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
5679
ArgEscapeData d = findArgEscapes(*this);
5681
if (d.occurrences == 0) {
5682
qWarning("QString::arg(): Argument missing: %s, %lld", toLocal8Bit().data(), a);
5687
if (d.occurrences > d.locale_occurrences)
5688
arg = number(a, base);
5691
if (d.locale_occurrences > 0) {
5693
locale_arg = locale.d->longLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
5696
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
5702
\a base is the base to use when converting the integer \a a into a
5703
string. \a base must be between 2 and 36, with 8 giving octal, 10
5704
decimal, and 16 hexadecimal numbers.
5706
QString QString::arg(qulonglong a, int fieldWidth, int base, const QChar &fillChar) const
5708
ArgEscapeData d = findArgEscapes(*this);
5710
if (d.occurrences == 0) {
5711
qWarning("QString::arg(): Argument missing: %s, %llu", toLocal8Bit().data(), a);
5716
if (d.occurrences > d.locale_occurrences)
5717
arg = number(a, base);
5720
if (d.locale_occurrences > 0) {
5722
locale_arg = locale.d->unsLongLongToString(a, -1, base, -1, QLocalePrivate::ThousandsGroup);
5725
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
5729
\fn QString QString::arg(short a, int fieldWidth, int base, const QChar &fillChar) const
5733
\a base is the base to use when converting the integer \a a into a
5734
string. \a base must be between 2 and 36, with 8 giving octal, 10
5735
decimal, and 16 hexadecimal numbers.
5739
\fn QString QString::arg(ushort a, int fieldWidth, int base, const QChar &fillChar) const
5743
\a base is the base to use when converting the integer \a a into a
5744
string. \a base must be between 2 and 36, with 8 giving octal, 10
5745
decimal, and 16 hexadecimal numbers.
5751
QString QString::arg(QChar a, int fieldWidth, const QChar &fillChar) const
5755
return arg(c, fieldWidth, fillChar);
5761
\a a is interpreded as a Latin-1 character.
5763
QString QString::arg(char a, int fieldWidth, const QChar &fillChar) const
5766
c += QLatin1Char(a);
5767
return arg(c, fieldWidth, fillChar);
5775
Argument \a a is formatted according to the \a fmt format specified,
5776
which is 'g' by default and can be any of the following:
5779
\header \i Format \i Meaning
5780
\row \i \c e \i format as [-]9.9e[+|-]999
5781
\row \i \c E \i format as [-]9.9E[+|-]999
5782
\row \i \c f \i format as [-]9.9
5783
\row \i \c g \i use \c e or \c f format, whichever is the most concise
5784
\row \i \c G \i use \c E or \c f format, whichever is the most concise
5787
With 'e', 'E', and 'f', \a prec is the number of digits after the
5788
decimal point. With 'g' and 'G', \a prec is the maximum number of
5789
significant digits (trailing zeroes are omitted).
5793
QString str = QString("delta: %1").arg(d, 0, 'E', 3);
5794
// str == "delta: 1.234E+01"
5797
The '%' can be followed by an 'L', in which case the sequence is
5798
replaced with a localized representation of \a a. The conversion uses
5799
the default locale, set by QLocale::setDefaultLocale(). If no default
5800
locale was specified, the "C" locale is used.
5802
QString QString::arg(double a, int fieldWidth, char fmt, int prec, const QChar &fillChar) const
5804
ArgEscapeData d = findArgEscapes(*this);
5806
if (d.occurrences == 0) {
5807
qWarning("QString::arg(): Argument missing: %s, %g", toLocal8Bit().data(), a);
5812
if (d.occurrences > d.locale_occurrences)
5813
arg = number(a, fmt, prec);
5816
if (d.locale_occurrences > 0) {
5819
QLocalePrivate::DoubleForm form = QLocalePrivate::DFDecimal;
5823
flags = QLocalePrivate::CapitalEorX;
5824
fmt = qToLower(fmt);
5828
form = QLocalePrivate::DFDecimal;
5831
form = QLocalePrivate::DFExponent;
5834
form = QLocalePrivate::DFSignificantDigits;
5837
#if defined(QT_CHECK_RANGE)
5838
qWarning("QString::arg: Invalid format char '%c'", fmt);
5843
flags |= QLocalePrivate::ThousandsGroup;
5845
locale_arg = locale.d->doubleToString(a, prec, form, -1, flags);
5848
return replaceArgEscapes(*this, d, fieldWidth, arg, locale_arg, fillChar);
5852
QString QString::multiArg(int numArgs, const QString **args) const
5857
int argForDigit[10];
5859
const QChar *uc = (const QChar*) d->data;
5860
const int len = d->size;
5861
const int end = len - 1;
5865
memset(digitUsed, 0, sizeof(digitUsed));
5867
for (i = 0; i < end; i++) {
5868
if (uc[i] == QLatin1Char('%')) {
5869
int digit = uc[i + 1].unicode() - '0';
5870
if (digit >= 0 && digit <= 9)
5875
for (i = 0; i < numArgs; i++) {
5878
} while (lastDigit < 10 && digitUsed[lastDigit] == 0);
5880
if (lastDigit == 10) {
5881
qWarning("QString::arg(): Argument missing: %s, %s", toLocal8Bit().data(), args[i]->toLocal8Bit().data());
5886
argForDigit[lastDigit] = i;
5891
if (uc[i] == QLatin1Char('%') && i != end) {
5892
int digit = uc[i + 1].unicode() - '0';
5893
if (digit >= 0 && digit <= lastDigit) {
5894
result += *args[argForDigit[digit]];
5908
void QString::updateProperties() const
5910
ushort *p = d->data;
5911
ushort *end = p + d->size;
5912
d->simpletext = true;
5915
// sort out regions of complex text formatting
5916
if (uc > 0x058f && (uc < 0x1100 || uc > 0xfb0f)) {
5917
d->simpletext = false;
5923
d->righttoleft = false;
5925
switch(::direction(*p))
5935
d->righttoleft = true;
5947
/*! \fn bool QString::isSimpleText() const
5952
/*! \fn bool QString::isRightToLeft() const
5957
/*! \fn QChar *QString::data()
5959
Returns a pointer to the data stored in the QString. The pointer
5960
can be used to access and modify the characters that compose the
5961
string. For convenience, the data is '\\0'-terminated.
5963
The pointer remains valid as long as the string isn't modified
5968
QString str = "Hello world";
5969
QChar *data = ba.data();
5971
cout << "[" + data->unicode() + "]" << endl;
5976
\sa constData(), operator[]()
5979
/*! \fn const QChar *QString::data() const
5984
/*! \fn const QChar *QString::constData() const
5986
Returns a pointer to the data stored in the QString. The pointer
5987
can be used to access the characters that compose the string. For
5988
convenience, the data is '\\0'-terminated.
5990
The pointer remains valid as long as the string isn't modified.
5992
\sa data(), operator[]()
5995
/*! \fn void QString::push_front(const QString &other)
5997
This function is provided for STL compatibility. It is equivalent
5998
to prepend(\a other).
6001
/*! \fn void QString::push_front(QChar ch)
6005
Same as prepend(\a ch).
6008
/*! \fn void QString::push_back(const QString &other)
6010
This function is provided for STL compatibility. It is equivalent
6011
to append(\a other).
6014
/*! \fn void QString::push_back(QChar ch)
6018
Same as append(\a ch).
6021
/*! \fn std::string QString::toStdString() const
6023
Returns a std::string object with the data contained in this
6024
QString. The Unicode data is converted into 8-bit characters
6027
This operator is mostly useful to pass a QString to a function
6028
that accepts a std::string object.
6030
If the QString contains non-ASCII Unicode characters, using this
6031
operator can lead to loss of information. You can disable this
6032
operator by defining \c QT_NO_CAST_TO_ASCII when you compile your
6033
applications. You then need to call toAscii() (or toLatin1() or
6034
toUtf8() or toLocal8Bit()) explicitly if you want to convert the data
6035
to \c{const char *} and pass the return value on to the
6036
std::string constructor.
6038
This operator is only available if Qt is configured with STL
6039
compabitility enabled.
6041
\sa toAscii(), toLatin1(), toUtf8(), toLocal8Bit()
6045
Constructs a QString that uses the first \a size Unicode
6046
characters in the array \a unicode. The data in \a unicode is \e
6047
not copied. The caller must be able to guarantee that \a unicode
6048
will not be deleted or modified as long as the QString (or an
6049
unmodified copy of it) exists.
6051
Any attempts to modify the QString or copies of it will cause it
6052
to create a deep copy of the data, ensuring that the raw data
6055
Here's an example of how we can use a QRegExp on raw data in
6056
memory without requiring to copy the data into a QString:
6059
static const QChar unicode[] = {
6060
0x005A, 0x007F, 0x00A4, 0x0060, 0x1009, 0x0020,
6064
int size = sizeof(unicode) / sizeof(QChar);
6066
QString str = QString::fromRawData(unicode, size);
6067
if (str.contains(QRegExp(pattern)))
6071
\warning A string created with fromRawData() is \e not
6072
'\\0'-terminated, unless the raw data contains a '\\0' character
6073
at position \a size. This means unicode() will \e not return a
6074
'\\0'-terminated string (although utf16() does, at the cost of
6075
copying the raw data).
6079
QString QString::fromRawData(const QChar *unicode, int size)
6081
Data *x = static_cast<Data *>(qMalloc(sizeof(Data)));
6083
x->data = (ushort *)unicode;
6089
x->alloc = x->size = size;
6091
x->clean = x->asciiCache = x->simpletext = x->righttoleft = 0;
6092
return QString(x, 0);
6095
/*! \class QLatin1String
6096
\brief The QLatin1String class provides a thin wrapper around an ASCII/Latin-1 encoded string literal.
6101
Many of QString's member functions are overloaded to accept
6102
\c{const char *} instead of QString. This includes the copy
6103
constructor, the assignment operator, the comparison operators,
6104
and various other functions such as \link QString::insert()
6105
insert() \endlink, \link QString::replace() replace()\endlink,
6106
and \link QString::indexOf() indexOf()\endlink. These functions
6107
are usually optimized to avoid constructing a QString object for
6108
the \c{const char *} data. For example, assuming \c str is a
6112
if (str == "auto" || str == "extern"
6113
|| str == "static" || str == "register") {
6121
if (str == QString("auto") || str == QString("extern")
6122
|| str == QString("static") || str == QString("register")) {
6127
because it doesn't construct four temporary QString objects and
6128
make a deep copy of the character data.
6130
Applications that define \c QT_NO_CAST_FROM_ASCII (as explained
6131
in the QString documentation) don't have access to QString's
6132
\c{const char *} API. To provide an efficient way of specifying
6133
constant Latin-1 strings, Qt provides the QLatin1String, which is
6134
just a very thin wrapper around a \c{const char *}. Using
6135
QLatin1String, the example code above becomes
6138
if (str == QLatin1String("auto")
6139
|| str == QLatin1String("extern")
6140
|| str == QLatin1String("static")
6141
|| str == QLatin1String("register") {
6146
This is a bit longer to type, but it provides exactly the same
6147
benefits as the first version of the code, and is faster than
6148
converting the Latin-1 strings using QString::fromLatin1().
6150
Thanks to the QString(const QLatin1String &) constructor,
6151
QLatin1String can be used everywhere a QString is expected. For example:
6154
QLabel *label = new QLabel(QLatin1String("MOD"), this);
6157
\sa QString, QLatin1Char
6160
/*! \fn QLatin1String::QLatin1String(const char *str)
6162
Constructs a QLatin1String object that stores \a str.
6164
The string data is \e not copied. The caller must be able to
6165
guarantee that \a str will not be deleted or modified as long as
6166
the QLatin1String object exists.
6171
/*! \fn const char *QLatin1String::latin1() const
6173
Returns the Latin-1 string stored in this object.
6176
/*! \fn bool QLatin1String::operator==(const QString &other) const
6178
Returns true if this string is equal to string \a other;
6179
otherwise returns false.
6181
The comparison is based exclusively on the numeric Unicode values
6182
of the characters and is very fast, but is not what a human would
6183
expect. Consider sorting user-interface strings with
6184
QString::localeAwareCompare().
6187
/*! \fn bool QLatin1String::operator!=(const QString &other) const
6189
Returns true if this string is not equal to string \a other;
6190
otherwise returns false.
6192
The comparison is based exclusively on the numeric Unicode values
6193
of the characters and is very fast, but is not what a human would
6194
expect. Consider sorting user-interface strings with
6195
QString::localeAwareCompare().
6198
/*! \fn bool QLatin1String::operator>(const QString &other) const
6200
Returns true if this string is lexically greater than string \a
6201
other; otherwise returns false.
6203
The comparison is based exclusively on the numeric Unicode values
6204
of the characters and is very fast, but is not what a human would
6205
expect. Consider sorting user-interface strings with
6206
QString::localeAwareCompare().
6209
/*! \fn bool QLatin1String::operator<(const QString &other) const
6211
Returns true if this string is lexically less than string \a
6212
other; otherwise returns false.
6214
The comparison is based exclusively on the numeric Unicode values
6215
of the characters and is very fast, but is not what a human would
6216
expect. Consider sorting user-interface strings with
6217
QString::localeAwareCompare().
6220
/*! \fn bool QLatin1String::operator>=(const QString &other) const
6222
Returns true if this string is lexically greater than or equal
6223
to string \a other; otherwise returns false.
6225
The comparison is based exclusively on the numeric Unicode values
6226
of the characters and is very fast, but is not what a human would
6227
expect. Consider sorting user-interface strings with
6228
QString::localeAwareCompare().
6231
/*! \fn bool QLatin1String::operator<=(const QString &other) const
6233
Returns true if this string is lexically less than or equal
6234
to string \a other; otherwise returns false.
6236
The comparison is based exclusively on the numeric Unicode values
6237
of the characters and is very fast, but is not what a human would
6238
expect. Consider sorting user-interface strings with
6239
QString::localeAwareCompare().
6242
#ifndef QT_NO_DATASTREAM
6246
Writes the string \a str to the stream \a out.
6248
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
6251
QDataStream &operator<<(QDataStream &out, const QString &str)
6253
if (out.version() == 1) {
6254
out << str.toLatin1();
6256
if (!str.isNull() || out.version() < 3) {
6257
int byteOrder = out.byteOrder();
6258
const QChar* ub = str.unicode();
6259
static const uint auto_size = 1024;
6262
if (str.length()*sizeof(QChar) > auto_size) {
6263
b = new char[str.length()*sizeof(QChar)];
6267
int l = str.length();
6270
if (byteOrder == QDataStream::BigEndian) {
6271
*c++ = (char)ub->row();
6272
*c++ = (char)ub->cell();
6274
*c++ = (char)ub->cell();
6275
*c++ = (char)ub->row();
6279
out.writeBytes(b, sizeof(QChar)*str.length());
6280
if (str.length()*sizeof(QChar) > auto_size)
6283
// write null marker
6284
out << (quint32)0xffffffff;
6293
Reads a string from the stream \a in into string \a str.
6295
\sa \link datastreamformat.html Format of the QDataStream operators \endlink
6298
QDataStream &operator>>(QDataStream &in, QString &str)
6300
#ifdef QT_QSTRING_UCS_4
6301
#if defined(Q_CC_GNU)
6302
#warning "operator>> not working properly"
6306
if (in.version() == 1) {
6309
str = QString::fromLatin1(l);
6312
in >> bytes; // read size of string
6313
if (bytes == 0xffffffff) { // null string
6315
} else if (bytes > 0) { // not empty
6318
in.setStatus(QDataStream::ReadCorruptData);
6322
const quint32 Step = 1024 * 1024;
6323
quint32 len = bytes / 2;
6324
quint32 allocated = 0;
6326
while (allocated < len) {
6327
int blockSize = qMin(Step, len - allocated);
6328
str.resize(allocated + blockSize);
6329
if (in.readRawData(reinterpret_cast<char *>(str.data()) + allocated * 2,
6330
blockSize * 2) != blockSize * 2) {
6332
in.setStatus(QDataStream::ReadPastEnd);
6335
allocated += blockSize;
6338
if ((in.byteOrder() == QDataStream::BigEndian)
6339
!= (QSysInfo::ByteOrder == QSysInfo::BigEndian)) {
6340
ushort *data = reinterpret_cast<ushort *>(str.data());
6342
*data = (*data >> 8) | (*data << 8);
6347
str = QLatin1String("");
6352
#endif // QT_NO_DATASTREAM
6355
\fn void QString::setLength(int nl)
6357
Use resize() instead.
6361
\fn QString QString::copy() const
6363
Use simple assignment instead. QString is implicitly shared so if
6364
a copy is modified only the copy is changed.
6368
\fn QString &QString::remove(QChar c, bool cs)
6370
Use the remove(QChar, Qt::CaseSensitive) overload instead.
6374
\fn QString &QString::remove(const QString &s, bool cs)
6376
Use the remove(QString, Qt::CaseSensitive) overload instead.
6380
\fn QString &QString::replace(QChar c, const QString &after, bool cs)
6382
Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
6386
\fn QString &QString::replace(const QString &before, const QString &after, bool cs)
6388
Use the replace(QString, QString, Qt::CaseSensitive) overload instead.
6392
\fn QString &QString::replace(char c, const QString &after, bool cs)
6394
Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
6398
\fn QString &QString::replace(char c, const QString &after, Qt::CaseSensitivity cs)
6400
Use the replace(QChar, QString, Qt::CaseSensitive) overload instead.
6404
\fn int QString::find(QChar c, int i = 0, bool cs = true) const
6406
Use indexOf() instead.
6410
\fn int QString::find(const QString &s, int i = 0, bool cs = true) const
6412
Use indexOf() instead.
6416
\fn int QString::findRev(QChar c, int i = -1, bool cs = true) const
6418
Use lastIndexOf() instead.
6422
\fn int QString::findRev(const QString &s, int i = -1, bool cs = true) const
6424
Use lastIndexOf() instead.
6428
\fn int QString::find(const QRegExp &rx, int i=0) const
6430
Use indexOf() instead.
6434
\fn int QString::findRev(const QRegExp &rx, int i=-1) const
6436
Use lastIndexOf() instead.
6440
\fn QBool QString::contains(QChar c, bool cs) const
6442
Use the contains(QChar, Qt::CaseSensitive) overload instead.
6446
\fn QBool QString::contains(const QString &s, bool cs) const
6448
Use the contains(QString, Qt::CaseSensitive) overload instead.
6452
\fn bool QString::startsWith(const QString &s, bool cs) const
6454
Use the startsWith(QString, Qt::CaseSensitive) overload instead.
6458
\fn bool QString::endsWith(const QString &s, bool cs) const
6460
Use the endsWith(QString, Qt::CaseSensitive) overload instead.
6464
\fn QString QString::leftJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const
6466
Use leftJustified() instead.
6470
\fn QString QString::rightJustify(int width, QChar fill = QLatin1Char(' '), bool trunc=false) const
6472
Use rightJustified() instead.
6476
\fn QString QString::lower() const
6478
Use toLower() instead.
6482
\fn QString QString::upper() const
6484
Use toUpper() instead.
6488
\fn QString QString::stripWhiteSpace() const
6490
Use trimmed() instead.
6494
\fn QString QString::simplifyWhiteSpace() const
6496
Use simplified() instead.
6500
\fn QString &QString::setUnicodeCodes(const ushort *unicode_as_ushorts, int size)
6502
Use setUtf16() instead.
6506
\fn ushort *QString::ucs2() const
6508
Use utf16() instead.
6512
\fn QString QString::fromUcs2(const ushort *unicode, int size = -1)
6514
Use fromUtf16() instead.
6518
\fn QString &QString::setAscii(const char *str, int len = -1)
6520
Use fromAscii() instead.
6524
\fn QString &QString::setLatin1(const char *str, int len = -1)
6526
Use fromLatin1() instead.
6530
\fn QChar QString::constref(uint i) const
6536
\fn QChar &QString::ref(uint i);
6538
Use operator[]() instead.
6542
\fn QString::operator const char *() const
6544
Use toAscii().constData() instead.