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

Qt学习之路(2)------Qt中的字符串类的更多相关文章

  1. Qt 学习之路 :Qt Quick Controls

    自 QML 第一次发布已经过去一年多的时间,但在企业应用领域,QML 一直没有能够占据一定地位.很大一部分原因是,QML 缺少一些在企业应用中亟需的组件,比如按钮.菜单等.虽然移动领域,这些组件已经变 ...

  2. Qt 学习之路 :Qt 绘制系统简介

    Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于QPainter,QPainterDevice和QPaintEngine三个类. QPainter用来执行绘制的 ...

  3. Qt 学习之路 :Qt 模块简介

    Qt 5 与 Qt 4 最大的一个区别之一是底层架构有了修改.Qt 5 引入了模块化的概念,将众多功能细分到几个模块之中.Qt 4 也有模块的概念,但是是一种很粗的划分,而 Qt 5 则更加细化.本节 ...

  4. Qt学习之路MainWindow学习过程中的知识点

    一.Qt的GUI程序有一个常用的顶层窗口,叫做MainWindow MainWindow继承自QMainWindow.QMainWindow窗口分成几个主要的区域:   二.QAction类 QAct ...

  5. Qt 学习之路:Qt 简介

    Qt 是一个著名的 C++ 应用程序框架.你并不能说它只是一个 GUI 库,因为 Qt 十分庞大,并不仅仅是 GUI 组件.使用 Qt,在一定程度上你获得的是一个“一站式”的解决方案:不再需要研究 S ...

  6. Qt 学习之路 :Qt 线程相关类

    希望上一章有关事件循环的内容还没有把你绕晕.本章将重新回到有关线程的相关内容上面来.在前面的章节我们了解了有关QThread类的简单使用.不过,Qt 提供的有关线程的类可不那么简单,否则的话我们也没必 ...

  7. QT学习之路--创建一个对话框

    Q_OBJECT:这是一个宏,凡是定义信号槽的类都必须声明这个宏. 函数tr()全名是QObject::tr(),被他处理过的字符串可以使用工具提取出来翻译成其他语言,也就是做国际化使用. 对于QT学 ...

  8. Qt学习之路

      Qt学习之路_14(简易音乐播放器)   Qt学习之路_13(简易俄罗斯方块)   Qt学习之路_12(简易数据管理系统)   Qt学习之路_11(简易多文档编辑器)   Qt学习之路_10(Qt ...

  9. Qt 学习之路 2(76):QML 和 QtQuick 2

    Home / Qt 学习之路 2 / Qt 学习之路 2(76):QML 和 QtQuick 2 Qt 学习之路 2(76):QML 和 QtQuick 2  豆子  2013年12月18日  Qt ...

  10. Qt 学习之路 2(74):线程和 QObject

    Home / Qt 学习之路 2 / Qt 学习之路 2(74):线程和 QObject Qt 学习之路 2(74):线程和 QObject  豆子  2013年12月3日  Qt 学习之路 2  2 ...

随机推荐

  1. 【CF492E】【数学】Vanya and Field

    Vanya decided to walk in the field of size n × n cells. The field contains m apple trees, the i-th a ...

  2. c++动态绑定与静态绑定

    C++为了支持多态性,采用了动态绑定和静态绑定 相关概念: 对象的静态类型:对象在声明时采用的类型,编译时确定 对象的动态类型:目前所指对象的类型,在运行时确定 class B { } class C ...

  3. DataReader 和 DataSet 的区别

    摘自:http://www.cnblogs.com/zhjjNo1/archive/2009/08/26/1554420.html 第一种解释 DataReader和DataSet最大的区别在于,Da ...

  4. HTML注释的一些规范

    HTMl里的一些注释符号 1.bady,head内部的注释:<!--放注释内容--> 2.css样式的注释:/*放注释的内容*/ 3.javascript注释 单行注释://放注释的内容 ...

  5. apache .htaccess 伪静态重定向,防盗链 限制下载...

    301全站跳转 RewriteEngine OnRewriteCond %{HTTP_HOST} ^www\.old\.net$ [NC]RewriteRule ^(.*)$ http://www.n ...

  6. Javascript自执行匿名函数(function() { })()的原理浅析

    匿名函数就是没有函数名的函数.这篇文章主要介绍了Javascript自执行匿名函数(function() { })()的原理浅析的相关资料,需要的朋友可以参考下 函数是JavaScript中最灵活的一 ...

  7. mysql添加超级管理员

    mysql>create user 'myroot'@'localhost' identified by 'myroot'; mysql>grant all privileges on * ...

  8. PHP面向对象的特性

    1.抽象性2.封装性3.继承extends4.多态

  9. [swift] NSClassFromString 无法获得该类

    在写OC的时候需要用 NSClassFromString(classStringName)获得一个类,如果存在就用这个类型来声明一个对象, 但是在swift的时候却往往得不到这个类,为什么呢? 从截图 ...

  10. Django中国|Django中文社区——python、django爱好者交流社区

    Django中国致力于成为Python和Django框架等技术的中文开发者学习交流平台. 内容涵盖python教程.python基础.Django教程.python入门.web.py教程.linux教 ...