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