Qt学习之路(2)------Qt中的字符串类

时间:2023-03-08 22:32:42
Qt学习之路(2)------Qt中的字符串类

QString

QString的一些基本用法

basic.cpp
#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString a = "love"; a.append(" chess");
a.prepend("I "); out << a << endl;
out << "The a string has " << a.count()
<< " characters" << endl; out << a.toUpper() << endl;
out << a.toLower() << endl; return 0;
}

Output

$ ./basic
I love chess
The a string has 12 characters
I LOVE CHESS
i love chess

字符串初始化

字符串有几种初始化方法

init.cpp
#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString str1 = "The night train";
out << str1 << endl; QString str2("A yellow rose");
out << str2 << endl; std::string s1 = "A blue sky";
QString str3 = s1.c_str();
out << str3 << endl; std::string s2 = "A thick fog";
QString str4 = QString::fromAscii(s2.data(), s2.size());
out << str4 << endl; char s3[] = "A deep forest";
QString str5(s3);
out << str5 << endl; return 0;
}

Output

$ ./init
The night train
A yellow rose
A blue sky
A thick fog
A deep forest

字符串元素访问

access.cpp

#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString a = "Eagle"; out << a[0] << endl;
out << a[4] << endl; out << a.at(0) << endl; if (a.at(5).isNull()) {
out << "Outside the range of the string" << endl;
} return 0;
}

Output

$ ./access
E
e
E
Outside the range of the string

求字符串长度

length.cpp

#include <QTextStream>

int main()
{
QTextStream out(stdout); QString s1 = "Eagle";
QString s2 = "Eagle\n";
QString s3 = "Eagle ";
QString s4 = "орел"; out << s1.length() << endl;
out << s2.length() << endl;
out << s3.length() << endl;
out << s4.length() << endl; return 0;
}

Output

$ ./length
5
6
6
8

字符串插值

interpolation.cpp
#include <QTextStream>

int main()
{
QTextStream out(stdout); QString s1 = "There are %1 white roses";
int n = 12; out << s1.arg(n) << endl; QString s2 = "The tree is %1m high";
double h = 5.65; out << s2.arg(h) << endl; QString s3 = "We have %1 lemons and %2 oranges";
int ln = 12;
int on = 4; out << s3.arg(ln).arg(on) << endl; return 0;
}

Output

$ ./interpolation
There are 12 white roses
The tree is 5.65m high
We have 12 lemons and 4 oranges

求子串

substrings.cpp

#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString str = "The night train"; out << str.right(5) << endl;
out << str.left(9) << endl;
out << str.mid(4, 5) << endl; QString str2("The big apple");
QStringRef sub(&str2, 0, 7); out << sub.toString() << endl; return 0;
}
Output
$ ./substrings
train
The night
night
The big

Looping through strings

遍历字符串中的字符

looping.cpp
#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString str = "There are many stars."; foreach (QChar qc, str) {
out << qc << " ";
} out << endl; for (QChar *it=str.begin(); it!=str.end(); ++it) {
out << *it << " " ;
} out << endl; for (int i = 0; i < str.size(); ++i) {
out << str.at(i) << " ";
} out << endl; return 0;
}

Output

$ ./looping
T h e r e a r e m a n y s t a r s .
T h e r e a r e m a n y s t a r s .
T h e r e a r e m a n y s t a r s .

字符串比较

comparing.cpp

#include <QTextStream>

#define STR_EQUAL 0

int main(void)
{
QTextStream out(stdout); QString a = "Rain";
QString b = "rain";
QString c = "rain\n"; if (QString::compare(a, b) == STR_EQUAL) {
out << "a, b are equal" << endl;
} else {
out << "a, b are not equal" << endl;
} out << "In case insensitive comparison:" << endl; if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) {
out << "a, b are equal" << endl;
} else {
out << "a, b are not equal" << endl;
} if (QString::compare(b, c) == STR_EQUAL) {
out << "b, c are equal" << endl;
} else {
out << "b, c are not equal" << endl;
} c.chop(1); out << "After removing the new line character" << endl; if (QString::compare(b, c) == STR_EQUAL) {
out << "b, c are equal" << endl;
} else {
out << "b, c are not equal" << endl;
} return 0;
}
Output
$ ./comparing
a, b are not equal
In case insensitive comparison:
a, b are equal
b, c are not equal
After removing the new line character
b, c are equal

字符串和其他类型进行转换

Output

#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString s1 = "12";
QString s2 = "15";
QString s3, s4; out << s1.toInt() + s2.toInt() << endl; int n1 = 30;
int n2 = 40; out << s3.setNum(n1) + s4.setNum(n2) << endl; return 0;
}

Output

$ ./converts
27
3040

字符类型

letters.cpp

#include <QTextStream>

int main(void)
{
QTextStream out(stdout); int digits = 0;
int letters = 0;
int spaces = 0;
int puncts = 0; QString str = "7 white, 3 red roses."; foreach(QChar s, str)
{
if (s.isDigit()) {
digits++;
} else if (s.isLetter()) {
letters++;
} else if (s.isSpace()) {
spaces++;
} else if (s.isPunct()) {
puncts++;
}
} out << QString("There are %1 characters").arg(str.count()) << endl;
out << QString("There are %1 letters").arg(letters) << endl;
out << QString("There are %1 digits").arg(digits) << endl;
out << QString("There are %1 spaces").arg(spaces) << endl;
out << QString("There are %1 punctuation characters").arg(puncts) << endl; return 0;
} Output
$ ./letters
There are 21 characters
There are 13 letters
There are 2 digits
There are 4 spaces
There are 2 punctuation characters

字符串修改

modify.cpp
#include <QTextStream>

int main(void)
{
QTextStream out(stdout); QString str = "Lovely";
str.append(" season"); out << str << endl; str.remove(10, 3);
out << str << endl; str.replace(7, 3, "girl");
out << str << endl; str.clear(); if (str.isEmpty()) {
out << "The string is empty" << endl;
} return 0;
}

Output

$ ./modify
Lovely season
Lovely sea
Lovely girl
The string is empty