source: trunk/doc/src/snippets/qstring/main.cpp@ 788

Last change on this file since 788 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 20.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtGui>
43#include <QApplication>
44#include <stdio.h>
45
46class Widget : public QWidget
47{
48public:
49 Widget(QWidget *parent = 0);
50
51 void constCharPointer();
52 void constCharArray();
53 void characterReference();
54 void atFunction();
55 void stringLiteral();
56 void modify();
57 void index();
58 QString boolToString(bool b);
59 void nullVsEmpty();
60
61 void appendFunction();
62 void argFunction();
63 void chopFunction();
64 void compareFunction();
65 void compareSensitiveFunction();
66 void containsFunction();
67 void countFunction();
68 void dataFunction();
69 void endsWithFunction();
70 void fillFunction();
71 void fromRawDataFunction();
72
73 void indexOfFunction();
74 void firstIndexOfFunction();
75 void insertFunction();
76 void isNullFunction();
77 void isEmptyFunction();
78 void lastIndexOfFunction();
79 void leftFunction();
80 void leftJustifiedFunction();
81 void leftRefFunction();
82 void midFunction();
83 void midRefFunction();
84 void numberFunction();
85
86 void prependFunction();
87 void removeFunction();
88 void replaceFunction();
89 void reserveFunction();
90 void resizeFunction();
91 void rightFunction();
92 void rightJustifiedFunction();
93 void rightRefFunction();
94 void sectionFunction();
95 void setNumFunction();
96 void simplifiedFunction();
97
98 void sizeFunction();
99 void splitFunction();
100 void splitCaseSensitiveFunction();
101 void sprintfFunction();
102 void startsWithFunction();
103 void toDoubleFunction();
104 void toFloatFunction();
105 void toIntFunction();
106 void toLongFunction();
107 void toLongLongFunction();
108
109 void toLowerFunction();
110 void toShortFunction();
111 void toUIntFunction();
112 void toULongFunction();
113 void toULongLongFunction();
114 void toUShortFunction();
115 void toUpperFunction();
116 void trimmedFunction();
117 void truncateFunction();
118
119 void plusEqualOperator();
120 void arrayOperator();
121};
122
123Widget::Widget(QWidget *parent)
124 : QWidget(parent)
125{
126}
127
128void Widget::constCharPointer()
129{
130//! [0]
131 QString str = "Hello";
132//! [0]
133}
134
135void Widget::constCharArray()
136{
137//! [1]
138 static const QChar data[4] = { 0x0055, 0x006e, 0x10e3, 0x03a3 };
139 QString str(data, 4);
140//! [1]
141}
142
143void Widget::characterReference()
144{
145//! [2]
146 QString str;
147 str.resize(4);
148
149 str[0] = QChar('U');
150 str[1] = QChar('n');
151 str[2] = QChar(0x10e3);
152 str[3] = QChar(0x03a3);
153//! [2]
154}
155
156void Widget::atFunction()
157{
158//! [3]
159 QString str;
160
161 for (int i = 0; i < str.size(); ++i) {
162 if (str.at(i) >= QChar('a') && str.at(i) <= QChar('f'))
163 qDebug() << "Found character in range [a-f]";
164 }
165//! [3]
166}
167
168void Widget::stringLiteral()
169{
170//! [4]
171 QString str;
172
173 if (str == "auto" || str == "extern"
174 || str == "static" || str == "register") {
175 // ...
176 }
177//! [4]
178}
179
180void Widget::modify()
181{
182//! [5]
183 QString str = "and";
184 str.prepend("rock "); // str == "rock and"
185 str.append(" roll"); // str == "rock and roll"
186 str.replace(5, 3, "&"); // str == "rock & roll"
187//! [5]
188}
189
190void Widget::index()
191{
192//! [6]
193 QString str = "We must be <b>bold</b>, very <b>bold</b>";
194 int j = 0;
195
196 while ((j = str.indexOf("<b>", j)) != -1) {
197 qDebug() << "Found <b> tag at index position" << j;
198 ++j;
199 }
200//! [6]
201}
202
203//! [7]
204 QString Widget::boolToString(bool b)
205 {
206 QString result;
207 if (b)
208 result = "True";
209 else
210 result = "False";
211 return result;
212 }
213//! [7]
214
215
216void Widget::nullVsEmpty()
217{
218//! [8]
219 QString().isNull(); // returns true
220 QString().isEmpty(); // returns true
221
222 QString("").isNull(); // returns false
223 QString("").isEmpty(); // returns true
224
225 QString("abc").isNull(); // returns false
226 QString("abc").isEmpty(); // returns false
227//! [8]
228}
229
230void Widget::appendFunction()
231{
232//! [9]
233 QString x = "free";
234 QString y = "dom";
235
236 x.append(y);
237 // x == "freedom"
238//! [9]
239
240//! [10]
241 x.insert(x.size(), y);
242//! [10]
243}
244
245void Widget::argFunction()
246{
247//! [11]
248 QString i; // current file's number
249 QString total; // number of files to process
250 QString fileName; // current file's name
251
252 QString status = QString("Processing file %1 of %2: %3")
253 .arg(i).arg(total).arg(fileName);
254//! [11]
255
256//! [12] //! [13]
257 QString str;
258//! [12]
259 str = "%1 %2";
260
261 str.arg("%1f", "Hello"); // returns "%1f Hello"
262 str.arg("%1f").arg("Hello"); // returns "Hellof %2"
263//! [13]
264
265//! [14]
266 str = QString("Decimal 63 is %1 in hexadecimal")
267 .arg(63, 0, 16);
268 // str == "Decimal 63 is 3f in hexadecimal"
269
270 QLocale::setDefault(QLocale(QLocale::English, QLocale::UnitedStates));
271 str = QString("%1 %L2 %L3")
272 .arg(12345)
273 .arg(12345)
274 .arg(12345, 0, 16);
275 // str == "12345 12,345 3039"
276//! [14]
277}
278
279void Widget::chopFunction()
280{
281//! [15]
282 QString str("LOGOUT\r\n");
283 str.chop(2);
284 // str == "LOGOUT"
285//! [15]
286}
287
288void Widget::compareFunction()
289{
290 int x = QString::compare("auto", "auto"); // x == 0
291 int y = QString::compare("auto", "car"); // y < 0
292 int z = QString::compare("car", "auto"); // z > 0
293}
294
295void Widget::compareSensitiveFunction()
296{
297//! [16]
298 int x = QString::compare("aUtO", "AuTo", Qt::CaseInsensitive); // x == 0
299 int y = QString::compare("auto", "Car", Qt::CaseSensitive); // y > 0
300 int z = QString::compare("auto", "Car", Qt::CaseInsensitive); // z < 0
301//! [16]
302}
303
304void Widget::containsFunction()
305{
306//! [17]
307 QString str = "Peter Pan";
308 str.contains("peter", Qt::CaseInsensitive); // returns true
309//! [17]
310}
311
312void Widget::countFunction()
313{
314//! [18]
315 QString str = "banana and panama";
316 str.count(QRegExp("a[nm]a")); // returns 4
317//! [18]
318}
319
320void Widget::dataFunction()
321{
322//! [19]
323 QString str = "Hello world";
324 QChar *data = str.data();
325 while (!data->isNull()) {
326 qDebug() << data->unicode();
327 ++data;
328 }
329//! [19]
330}
331
332void Widget::endsWithFunction()
333{
334//! [20]
335 QString str = "Bananas";
336 str.endsWith("anas"); // returns true
337 str.endsWith("pple"); // returns false
338//! [20]
339}
340
341void Widget::fillFunction()
342{
343//! [21]
344 QString str = "Berlin";
345 str.fill('z');
346 // str == "zzzzzz"
347
348 str.fill('A', 2);
349 // str == "AA"
350//! [21]
351}
352
353void Widget::fromRawDataFunction()
354{
355//! [22]
356 QRegExp pattern;
357 static const QChar unicode[] = {
358 0x005A, 0x007F, 0x00A4, 0x0060,
359 0x1009, 0x0020, 0x0020};
360 int size = sizeof(unicode) / sizeof(QChar);
361
362 QString str = QString::fromRawData(unicode, size);
363 if (str.contains(QRegExp(pattern))) {
364 // ...
365//! [22] //! [23]
366 }
367//! [23]
368}
369
370void Widget::indexOfFunction()
371{
372//! [24]
373 QString x = "sticky question";
374 QString y = "sti";
375 x.indexOf(y); // returns 0
376 x.indexOf(y, 1); // returns 10
377 x.indexOf(y, 10); // returns 10
378 x.indexOf(y, 11); // returns -1
379//! [24]
380}
381
382void Widget::firstIndexOfFunction()
383{
384//! [25]
385 QString str = "the minimum";
386 str.indexOf(QRegExp("m[aeiou]"), 0); // returns 4
387//! [25]
388}
389
390void Widget::insertFunction()
391{
392//! [26]
393 QString str = "Meal";
394 str.insert(1, QString("ontr"));
395 // str == "Montreal"
396//! [26]
397}
398
399void Widget::isEmptyFunction()
400{
401//! [27]
402 QString().isEmpty(); // returns true
403 QString("").isEmpty(); // returns true
404 QString("x").isEmpty(); // returns false
405 QString("abc").isEmpty(); // returns false
406//! [27]
407}
408
409void Widget::isNullFunction()
410{
411//! [28]
412 QString().isNull(); // returns true
413 QString("").isNull(); // returns false
414 QString("abc").isNull(); // returns false
415//! [28]
416}
417
418void Widget::lastIndexOfFunction()
419{
420//! [29]
421 QString x = "crazy azimuths";
422 QString y = "az";
423 x.lastIndexOf(y); // returns 6
424 x.lastIndexOf(y, 6); // returns 6
425 x.lastIndexOf(y, 5); // returns 2
426 x.lastIndexOf(y, 1); // returns -1
427//! [29]
428
429//! [30]
430 QString str = "the minimum";
431 str.lastIndexOf(QRegExp("m[aeiou]")); // returns 8
432//! [30]
433}
434
435void Widget::leftFunction()
436{
437//! [31]
438 QString x = "Pineapple";
439 QString y = x.left(4); // y == "Pine"
440//! [31]
441}
442
443void Widget::leftJustifiedFunction()
444{
445//! [32]
446 QString s = "apple";
447 QString t = s.leftJustified(8, '.'); // t == "apple..."
448//! [32]
449
450//! [33]
451 QString str = "Pineapple";
452 str = str.leftJustified(5, '.', true); // str == "Pinea"
453//! [33]
454}
455
456void Widget::midFunction()
457{
458//! [34]
459 QString x = "Nine pineapples";
460 QString y = x.mid(5, 4); // y == "pine"
461 QString z = x.mid(5); // z == "pineapples"
462//! [34]
463}
464
465void Widget::numberFunction()
466{
467//! [35]
468 long a = 63;
469 QString s = QString::number(a, 16); // s == "3f"
470 QString t = QString::number(a, 16).toUpper(); // t == "3F"
471//! [35]
472}
473
474void Widget::prependFunction()
475{
476//! [36]
477 QString x = "ship";
478 QString y = "air";
479 x.prepend(y);
480 // x == "airship"
481//! [36]
482}
483
484void Widget::removeFunction()
485{
486//! [37]
487 QString s = "Montreal";
488 s.remove(1, 4);
489 // s == "Meal"
490//! [37]
491
492//! [38]
493 QString t = "Ali Baba";
494 t.remove(QChar('a'), Qt::CaseInsensitive);
495 // t == "li Bb"
496//! [38]
497
498//! [39]
499 QString r = "Telephone";
500 r.remove(QRegExp("[aeiou]."));
501 // r == "The"
502//! [39]
503}
504
505void Widget::replaceFunction()
506{
507//! [40]
508 QString x = "Say yes!";
509 QString y = "no";
510 x.replace(4, 3, y);
511 // x == "Say no!"
512//! [40]
513
514//! [41]
515 QString str = "colour behaviour flavour neighbour";
516 str.replace(QString("ou"), QString("o"));
517 // str == "color behavior flavor neighbor"
518//! [41]
519
520//! [42]
521 QString s = "Banana";
522 s.replace(QRegExp("a[mn]"), "ox");
523 // s == "Boxoxa"
524//! [42]
525
526//! [43]
527 QString t = "A <i>bon mot</i>.";
528 t.replace(QRegExp("<i>([^<]*)</i>"), "\\emph{\\1}");
529 // t == "A \\emph{bon mot}."
530//! [43]
531
532//! [86]
533 QString equis = "xxxxxx";
534 equis.replace("xx", "x");
535 // equis == "xxx"
536//! [86]
537}
538
539void Widget::reserveFunction()
540{
541//! [44]
542 QString result;
543 int maxSize;
544 bool condition;
545 QChar nextChar;
546
547 result.reserve(maxSize);
548
549 while (condition)
550 result.append(nextChar);
551
552 result.squeeze();
553//! [44]
554}
555
556void Widget::resizeFunction()
557{
558//! [45]
559 QString s = "Hello world";
560 s.resize(5);
561 // s == "Hello"
562
563 s.resize(8);
564 // s == "Hello???" (where ? stands for any character)
565//! [45]
566
567//! [46]
568 QString t = "Hello";
569 t += QString(10, 'X');
570 // t == "HelloXXXXXXXXXX"
571//! [46]
572
573//! [47]
574 QString r = "Hello";
575 r = r.leftJustified(10, ' ');
576 // r == "Hello "
577//! [47]
578}
579
580void Widget::rightFunction()
581{
582//! [48]
583 QString x = "Pineapple";
584 QString y = x.right(5); // y == "apple"
585//! [48]
586}
587
588void Widget::rightJustifiedFunction()
589{
590//! [49]
591 QString s = "apple";
592 QString t = s.rightJustified(8, '.'); // t == "...apple"
593//! [49]
594
595//! [50]
596 QString str = "Pineapple";
597 str = str.rightJustified(5, '.', true); // str == "Pinea"
598//! [50]
599}
600
601void Widget::sectionFunction()
602{
603//! [51] //! [52]
604 QString str;
605//! [51]
606 QString csv = "forename,middlename,surname,phone";
607 QString path = "/usr/local/bin/myapp"; // First field is empty
608 QString::SectionFlag flag = QString::SectionSkipEmpty;
609
610
611 str = csv.section(',', 2, 2); // str == "surname"
612 str = path.section('/', 3, 4); // str == "bin/myapp"
613 str = path.section('/', 3, 3, flag); // str == "myapp"
614//! [52]
615
616//! [53]
617 str = csv.section(',', -3, -2); // str == "middlename,surname"
618 str = path.section('/', -1); // str == "myapp"
619//! [53]
620
621//! [54]
622 QString data = "forename**middlename**surname**phone";
623
624 str = data.section("**", 2, 2); // str == "surname"
625 str = data.section("**", -3, -2); // str == "middlename**surname"
626//! [54]
627
628//! [55]
629 QString line = "forename\tmiddlename surname \t \t phone";
630 QRegExp sep("\\s+");
631 str = line.section(sep, 2, 2); // s == "surname"
632 str = line.section(sep, -3, -2); // s == "middlename surname"
633//! [55]
634}
635
636void Widget::setNumFunction()
637{
638//! [56]
639 QString str;
640 str.setNum(1234); // str == "1234"
641//! [56]
642}
643
644void Widget::simplifiedFunction()
645{
646//! [57]
647 QString str = " lots\t of\nwhitespace\r\n ";
648 str = str.simplified();
649 // str == "lots of whitespace";
650//! [57]
651}
652
653void Widget::sizeFunction()
654{
655//! [58]
656 QString str = "World";
657 int n = str.size(); // n == 5
658 str.data()[0]; // returns 'W'
659 str.data()[4]; // returns 'd'
660 str.data()[5]; // returns '\0'
661//! [58]
662}
663
664void Widget::splitFunction()
665{
666//! [59]
667 QString str;
668 QStringList list;
669
670 str = "Some text\n\twith strange whitespace.";
671 list = str.split(QRegExp("\\s+"));
672 // list: [ "Some", "text", "with", "strange", "whitespace." ]
673//! [59]
674
675//! [60]
676 str = "This time, a normal English sentence.";
677 list = str.split(QRegExp("\\W+"), QString::SkipEmptyParts);
678 // list: [ "This", "time", "a", "normal", "English", "sentence" ]
679//! [60]
680
681//! [61]
682 str = "Now: this sentence fragment.";
683 list = str.split(QRegExp("\\b"));
684 // list: [ "", "Now", ": ", "this", " ", "sentence", " ", "fragment", "." ]
685//! [61]
686}
687
688void Widget::splitCaseSensitiveFunction()
689{
690//! [62]
691 QString str = "a,,b,c";
692
693 QStringList list1 = str.split(",");
694 // list1: [ "a", "", "b", "c" ]
695
696 QStringList list2 = str.split(",", QString::SkipEmptyParts);
697 // list2: [ "a", "b", "c" ]
698//! [62]
699}
700
701void Widget::sprintfFunction()
702{
703//! [63]