目录
在本章中,我们使用字符串。 Qt5 有一个用于处理字符串的 QString 类。它非常强大,并且有很多方法。
QString 类提供了一个 Unicode 字符串。它将字符串存储为 16 位 QChars。每个 QChar 对应一个 Unicode 4.0 字符。与许多其他编程语言中的字符串不同,QString 可以被修改。
在本章的例子中,我们不需要 Qt GUI 模块;我们创建命令行程序。由于默认包含 Qt GUI,我们可以通过在项目文件中添加 QT -= gui 声明来禁用它。
Qt5字符串基础示例
在第一个示例中,我们使用 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;
}
在代码示例中,我们初始化了一个 QString。 我们附加和前置一些额外的文本。 我们打印字符串的长度。 最后,我们以大写和小写形式打印修改后的字符串。
QString a { "love" };
初始化了一个 QString。
a.append(" chess"); a.prepend("I ");
我们将文本附加和前置到初始字符串。 字符串就地修改。
out << a << endl;
“I love chess”被打印到终端。
out << "The a string has " << a.count() << " characters" << endl;
count 方法返回字符串中的字符数。 length 和 size 方法是等价的。
out << a.toUpper() << endl; out << a.toLower() << endl;
这两个方法返回字符串的大写和小写副本。 他们不修改字符串,他们返回字符串的新修改副本。
$ ./basic
I love chess
The a string has 12 characters
I LOVE CHESS
i love chess
Qt5 初始化字符串
可以通过多种方式初始化 QString。
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;
QString str3 {"An old falcon"};
out << str3 << endl;
std::string s1 = "A blue sky";
QString str4 = s1.c_str();
out << str4 << endl;
std::string s2 = "A thick fog";
QString str5 = QString::fromLatin1(s2.data(), s2.size());
out << str5 << endl;
char s3[] = "A deep forest";
QString str6(s3);
out << str6 << endl;
return 0;
}
我们介绍了五种初始化 QString 的方法。
QString str1 = "The night train";
这是在计算机语言中初始化字符串的传统方式。
QString str2("A yellow rose");
这是初始化 QString 的对象方法。
QString str3 {"An old falcon"};
这是大括号初始化。
std::string s1 = "A blue sky"; QString str4 = s1.c_str();
我们有一个来自 C++ 标准库的字符串对象。我们使用它的 c_str 方法来生成一个以 null 结尾的字符序列。这个字符数组是字符串的经典 C 表示,可以分配给 QString 变量。
std::string s2 = "A thick fog"; QString str5 = QString::fromLatin1(s2.data(), s2.size());
在这些代码行中,我们将标准 C++ 字符串转换为 QString。我们使用 fromLatin1 方法。它需要一个指向字符数组的指针。指针与 std::string 的数据方法一起返回。第二个参数是 std::string 的大小。
char s3[] = "A deep forest"; QString str6(s3);
这是一个 C 字符串;它是一个字符数组。其中一个 QString 构造函数可以将字符数组作为参数。
$ ./init The night train A yellow rose An old falcon A blue sky A thick fog A deep forest
Qt5访问字符串元素
QString 是 QChars 的序列。 可以使用 [] 运算符或 at 方法访问字符串的元素。
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;
}
我们从特定的 QString 打印一些单独的字符。
out << a[0] << endl; out << a[4] << endl;
我们打印字符串的第一个和第五个元素。
out << a.at(0) << endl;
使用 at 方法,我们检索字符串的第一个字符。
if (a.at(5).isNull()) { out << "Outside the range of the string" << endl; }
如果我们试图访问字符串字符范围之外的字符,at 方法将返回 null。
$ ./access E e E Outside the range of the string
Qt5字符串长度
获取字符串长度的三种方法。大小、计数和长度方法。所有人都做同样的事情。它们返回指定字符串中的字符数。
length.cpp
#include <QTextStream>
int main(void) {
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;
}
我们得到四个字符串的大小。
QString s2 = "Eagle\n"; QString s3 = "Eagle ";
这两个字符串中的每一个都有一个空白字符。
QString s4 = "орел";
该字符串由俄语字母组成。
$ ./length 5 6 6 4
从输出中我们可以看到 length 方法也计算了空白字符。
Qt5 构建字符串
动态字符串构建允许我们用实际值替换特定的控制字符。 我们使用 arg 方法进行插值。
building.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 %1 m 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;
}
将被替换的标记以 % 字符开头。 后面的字符是指定参数的数字。 一个字符串可以有多个参数。 arg 方法被重载,它可以接受整数、长数字、字符和 QChars 等。
QString s1 = "There are %1 white roses"; int n = 12;
%1 是我们计划替换的标记。 我们定义了一个整数。
out << s1.arg(n) << endl;
arg 方法接受一个整数。 %1 标记被替换为 n 变量的值。
QString s2 = "The tree is %1 m high"; double h = 5.65; out << s2.arg(h) << endl; 这三行对double做同样的事情。 自动调用正确的 arg 方法。
QString s3 = "We have %1 lemons and %2 oranges"; int ln = 12; int on = 4; out << s3.arg(ln).arg(on) << endl;
我们可以有多个控制字符。 %1 引用第一个参数,%2 引用第二个参数。 arg 方法在连续链中调用。
$ ./building There are 12 white roses The tree is 5.65 m high We have 12 lemons and 4 oranges
Qt5 子字符串
在进行文本处理时,我们需要找到普通字符串的子字符串。我们有 left、mid 和 right 方法供我们使用。
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;
}
我们将使用所有三种方法来查找给定字符串的某些子字符串。
out << str.right(5) << endl;
使用right,我们得到了 str 字符串最右边的五个字符。 “train”被打印出来。
out << str.left(9) << endl;
使用 left 方法,我们得到 str 字符串最左边的九个字符。打印了“The night”。
out << str.mid(4, 5) << endl;
使用 mid 方法,我们从第四个位置开始得到五个字符。打印“night”。
QString str2("The big apple"); QStringRef sub(&str2, 0, 7);
QStringRef 类是 QString 的只读版本。这里我们创建了 str2 字符串的一部分的 QStringRef。第二个参数是位置,第三个是子串的长度。
$ ./substrings train The night night The big
Qt5循环遍历字符串
QString 由 QChars 组成。我们可以循环遍历 QString 来访问字符串的每个元素。
looping.cpp
#include <QTextStream>
int main(void) {
QTextStream out(stdout);
QString str { "There are many stars." };
for (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;
}
我们展示了三种遍历 QString 的方法。当我们将它们打印到终端时,我们在字母之间添加一个空格字符。
for (QChar qc: str) { out << qc << " "; }
我们使用基于范围的 for 循环遍历字符串。
for (QChar *it=str.begin(); it!=str.end(); ++it) { out << *it << " " ; }
在此代码中,我们使用迭代器遍历字符串。
for (int i = 0; i < str.size(); ++i) { out << str.at(i) << " "; }
$ ./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 .
Qt5字符串比较
QString::compare 静态方法用于比较两个字符串。该方法返回一个整数。如果返回值小于零,则第一个字符串小于第二个。如果它返回零,则两个字符串相等。最后,如果返回值大于零,则第一个字符串大于第二个。 “less”是指字符串的特定字符在字符表中位于另一个字符之前。
字符串的比较方式如下:比较两个字符串的第一个字符;如果它们相等,则比较下面两个字符,直到我们找到一些不同的字符或者我们发现所有字符都匹配。
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;
}
我们将使用比较方法进行区分大小写和不区分大小写的比较。
#define STR_EQUAL 0
为了更好的代码清晰度,我们定义了 STR_EQUAL 常量。
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; }
我们比较 a 和 b 字符串,它们不相等。它们在第一个字符上有所不同。
if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) { out << "a, b are equal" << endl; } else { out << "a, b are not equal" << endl; }
在不区分大小写的比较中,字符串是相等的。 Qt::CaseInsensitive 使比较不区分大小写。
c.chop(1);
chop 方法从 c 字符串中删除最后一个字符。现在 b 和 c 字符串相等。
$ ./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
Qt5 转换字符串
字符串通常需要转换为其他数据类型,反之亦然。 toInt、toFloat、toLong 是三个 QString 方法,可将字符串转换为整数、浮点数和长整数。 (还有更多这样的方法。) setNum 方法将各种数字数据类型转换为字符串。方法被重载并自动调用正确的方法。
converting.cpp
#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;
}
在示例中,我们将两个字符串转换为整数并将它们相加。然后我们将两个整数转换为字符串并将它们连接起来。
out << s1.toInt() + s2.toInt() << endl;
toInt 方法将字符串转换为整数。我们添加两个从字符串转换而来的数字。
out << s3.setNum(n1) + s4.setNum(n2) << endl;
在这种情况下,setNum 方法将整数转换为字符串。我们连接两个字符串。
$ ./converting 27 3040
字母
字符被分为各种类别:数字、字母、空格和标点符号。每个QString由QChars组成。QChar有isDigit、isLetter、isSpace和isPunct方法来完成工作。
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." };
for (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;
}
在这个例子中,我们定义了一个简单的句子。我们计算该句子中的数字、字母、空格和标点符号的数量。
int digits = 0; int letters = 0; int spaces = 0; int puncts = 0;
我们为每个字符类别定义一个整数变量。
QString str { "7 white, 3 red roses." };
这是需要检查的句子。
for (QChar s : str) { if (s.isDigit()) { digits++; } else if (s.isLetter()) { letters++; } else if (s.isSpace()) { spaces++; } else if (s.isPunct()) { puncts++; } }
我们使用基于for-range的循环来浏览QString。每个元素都是一个QChar。我们使用QChar类的方法来确定字符的类别。
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;
使用字符串插值,我们将这些数字打印到终端。
$ ./letters There are 21 characters There are 13 letters There are 2 digits There are 4 spaces There are 2 punctuation characters
Qt5修改字符串
一些方法(例如toLower方法)返回原始字符串的一个新的修改副本。其他方法则是在原地修改字符串。我们介绍其中的一些。
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;
}
我们描述了四个就地修改字符串的方法。
str.append(" season")。
append方法在字符串的末尾添加一个新的字符串。
str.remove(10, 3);
remove方法从字符串中删除3个字符,从第10位开始。
str.replace(7, 3, "girl");
替换方法用指定的字符串替换了从位置7开始的3个字符。
str.clear();
clear方法清除了字符串。
$ ./modify Lovely season Lovely sea Lovely girl The string is empty
Qt5对齐字符串
有一个整齐的输出是一个常见的要求。我们可以使用leftJustified和rightJustified方法来对齐我们的字符串。
right_align.cpp
#include <QTextStream>
int main(void) {
QTextStream out(stdout);
QString field1 { "Name: " };
QString field2 { "Occupation: " };
QString field3 { "Residence: " };
QString field4 { "Marital status: " };
int width = field4.size();
out << field1.rightJustified(width, ' ') << "Robert\n";
out << field2.rightJustified(width, ' ') << "programmer\n";
out << field3.rightJustified(width, ' ') << "New York\n";
out << field4.rightJustified(width, ' ') << "single\n";
return 0;
}
该例子将字段字符串向右对齐。
int width = field4.size()。
我们计算出最宽的字符串的大小。
out << field1.rightJustified(width, ' ') << "Robert\n";
rightJustified方法返回一个有宽度字符的字符串。如果字符串更短,剩下的部分就用提供的字符填充。在我们的例子中,它是一个空格字符。
$ ./right_align Name: Robert Occupation: programmer Residence: New York Marital status: single
Qt5转义字符
Qt5有一个toHtmlEscaped方法,可以将纯文本字符串转换为HTML字符串,并将HTML元字符<, >, &, 和 "替换为HTML命名实体。
$ cat cprog.c
#include <stdio.h>
int main(void) {
for (int i=1; i<=10; i++) {
printf("Bottle %d\n", i);
}
}
这个C语言程序包括HTML元字符。
html_escape.cpp
#include <QTextStream>
#include <QFile>
int main(void) {
QTextStream out(stdout);
QFile file("cprog.c");
if (!file.open(QIODevice::ReadOnly)) {
qWarning("Cannot open file for reading");
return 1;
}
QTextStream in(&file);
QString allText = in.readAll();
out << allText.toHtmlEscaped() << endl;
file.close();
return 0;
}
这个例子读取了一个C语言程序,并将元字符替换成了它们的命名实体。
在本章中,我们在Qt5中使用了字符串。