大家好,我是你们的工具人老吴。
今天用几个小例子,
帮忙大家快速了解一下 Qt 里如何用 QString 完成几个最高频的字符串操作。
开门见山
#include
int main(void)
{
QTextStream out(stdout);
// 1. traditional way
QString str1 = "A night train";
out ;
// 2. object way
QString str2("A yellow rose");
out
// 3. brace initialization
QString str3 {"An old falcon"};
out
// 4. std:string to QString
std::string s1 = "A blue sky";
QString str4 = s1.c_str();
out
// 5. convert a standard C++ string to a QString
std::string s2 = "A thick fog";
QString str5 = QString::fromLatin1(s2.data(), s2.size());
out
// 6.
char s3[] = "A deep forest";
QString str6(s3);
out
return 0;
}
运行效果:
A night train
A yellow rose
An old falcon
A blue sky
A thick fog
A deep forest
Press to close this window...
6 种常用的初始化 QString 的方式:
-
traditional way,传统方式,注意这是初始化、不是赋值;
-
object way,跟 1 没有差别,都会调用构造函数;
-
brace initialization,大括号方式,C++11 提出的统一初始化语法;
-
-
用 QString::fromLatin1(std::string.data(), std::string.size()) 初始化;
-
用 C 中的 null-terminated 字符数组初始化;
访问元素
#include
int main(void)
{
QTextStream out(stdout);
QString a { "Eagle" };
out <0] out <4]
out <0)
if (a.at(5).isNull()) {
out <"Outside the range of the string" }
return 0;
}
运行效果:
E
e
E
ASSERT: "uint(i) in file /opt/Qt5.14.1/5.14.1/gcc_64/include/QtCore/qstring.h, line 1029
Press to close this window...
QString 是 QChars 的序列, 2 种访问其元素的方法:
-
-
它们的区别是,用 [] 的话要自行检查下标的有效性。
获取长度
#include
int main(void)
{
QTextStream out(stdout);
QString s1 = "Eagle";
QString s2 = "Eagle\n";
QString s3 = "Eagle "
;
out ;
out out
return 0;
}
运行效果:
5
6
6
Press to close this window...
\n 和空格都会被算上。
动态构建
#include
int main()
{
QTextStream out(stdout);
QString s1 = "There are %1 white roses";
int n = 12;
out ;
QString s2 = "The tree is %1 m high";
double h = 5.65;
out
QString s3 = "We have %1 lemons and %2 oranges";
int ln = 12;
int on = 4;
out
return 0;
}
运行效果:
There are 12 white roses
The tree is 5.65 m high
We have 12 lemons and 4 oranges
Press to close this window...
%1、%2 被称为是 marker,我们可以通过 arg() 可以将 marker 替换成我们想要的内容。
如果你想动态地构建字符串的话,这个函数非常好用。
但是,如果需要大量重复地构建字符串的话,相比 sprintf(),Qstring::arg() 可能会存在性能问题。
提取子串
#include
int main(void)
{
QTextStream out(stdout);
QString str = { "The night train" };
out <5) out <9) out <4, 5)
QString str2("The big apple");
QStringRef sub(&str2, 0, 7);
out
return 0;
}
运行效果:
train
The night
night
The big
Press to close this window...
3 种切法:
左切、右切、从中间切。
QStringRef 就是只读版本的 QString,一般用来指向某个 QString 的子串,这个类就是专门为了提升子串处理的性能而设计的。
分割处理
#include
int main(void)
{
QTextStream out(stdout);
int i;
QString str = "a,,b,c";
QStringList list1 = str.split(',');
for(i=0; i out<": "<endl;
}
}
运行效果:
0: a
1:
2: b
3: c
Press to close this window...
遍历字符串
#include
int main(void)
{
QTextStream out(stdout);
QString str { "There are many stars." };
// 1. range-based for loop
for (QChar qc: str) {
out <" ";
}
out
// 2. iterators
for (QChar *it=str.begin(); it!=str.end(); ++it) {
out <" " ;
}
out
// 3. QString::size() + QString::at()
for (int i = 0; i out <" ";
}
out
return 0;
}
运行效果:
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 .
Press to close this window...
QString 由 QChars 构成,上面列举了 3 种遍历 QString 的方式。
比较字符串
#include
#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" } else {
out <"a, b are not equal" }
if (QString::compare(a, b, Qt::CaseInsensitive) == STR_EQUAL) {
out <"a, b are equal in case insensitive comparison" } else {
out <"a, b are not equal" }
if (b == c) {
out <"b, c are equal" } else {
out <"b, c are not equal" }
return 0;
}
运行效果:
a, b are not equal
a, b are equal in case insensitive comparison
b, c are not equal
Press to close this window...
比较字符串的 2 种方式:
-
-
用静态函数 QString::compare();
修改内容
#include
int main(void)
{
QTextStream out(stdout);
QString str { "Lovely" };
str.append(" season");
out ;
str.remove(10