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. ReactNative-----环境搭建二(android)

    一.初始化一个ReactNative项目 在指定目录运行命令:react-native init Vince(项目名称)  //其过程就是在使用CLI工具构建项目, 命令行代码 F:\React> ...

  2. thinkphp 模板中赋值

    在项目开发的时候,有时候希望直接在模板中调用 一些自定义方法,或者内置方法来,处理获得一些数据,并且赋值给一个变量给后面调用,这个时候如果用原生Php 的方式调用如下:<?php $abc = ...

  3. 关于web项目中中文乱码问题的总结

    关于post和get的中文乱码处理 get: (1)转码:String username=request.getParameter("username");       Strin ...

  4. 某Python群的入群题目

    为了确保不被通过搜索引擎直接搜索题目搜出来,我重新描述下题目: 给n, 求1~n的每个数的约数和 每个约数出现的个数是 n // i个, 出现x次的约数范围是[n // (i + 1) + 1, n ...

  5. 快速排序 javascript实现

    Quicksort(快速排序) 是由 东尼·霍尔 所发展的一种排序. 它比其他的Ο(n log n)算法更快, 因为它的内部循环(inner loop)可以在大部分的架构上很有效率地被实现出来.当然, ...

  6. Page类成员

    1. Request,Response,Server属性:对contex.Request,context.Response,context.Server的简化调用2. AppRelativeVirtu ...

  7. Python中 if __name__ == '__main__': 详解

    一个python文件就可以看作是一个python的模块,这个python模块(.py文件)有两种使用方式:直接运行和作为模块被其他模块调用. __name__:每一个模块都有一个内置属性__name_ ...

  8. thinkphp分页格式的完全自定义,直接输入数字go到输入数字页

    实现分页效果如下: 以下标注红色字体的为重点   找到文件page.class.php在ThinkPHP/Library/Thinkpage.class.php并打开文件,复制函数show,在本文件中 ...

  9. SQLServer数据库 导出表和导入sql脚本

    1.选择需要导出表的数据库--任务---生成脚本 2.显示:生成和发布脚本窗口--简介(某些可能关闭该页面的,可以省略该步骤),点击下一步 3.显示:生成和发布脚本窗口--选择对象--按照图片操作即可 ...

  10. Django生产环境的部署-Apache-mod_wsgi

    httpd.conf配置 ServerSignature On ServerTokens Full Define APACHE24 Apache2.4 Define SERVER_BASE_DIR & ...