Qt学习之路7--字符串类QString

简介

字符串的概念在C语言中就存在,有一个历史遗留问题,就是C语言其实是不支持真正意义上的字符串,它是通过字符数组和一组函数实现字符串的操作。
但是在C到C++的进化之后C++已经支持通过定义类来定义自定义类型,这样就可以自定义字符串类,但是在C++发布时一同发布了STL标准库,其中包含了字符串类std::string类类型。

STL全名叫标准模板库,其标准体现在三个方面

  • 相同的全局函数
  • 相同的算法类和数据结构类
  • 相同的类成员函数
    因为STL库的实现依赖于不同厂商实现的编译器,在实现上可能存在差异,所以依赖STL开发的C++程序在不同的平台上行为可能存在差异!!!

Qt中的字符串类

  • 采用Unicode编码,所以一个QChar占用两个字节
  • 使用隐式共享技术来节省内存和减少不必要的数据拷贝
  • 跨平台使用,不用考虑字符串的平台兼容性
  • QString直接支持字符串和数字之间的相互转换
  • QString直接支持字符串之间的大小比较(按照字典序)
  • QString直接支持不同编码下的字符串转换
  • QString直接支持std::string和std::wstring之间的相互转换
  • QString直接支持正则表达式的使用

QString常用操作

包括字符串类对象构造、追加字符串、组合字符串、插入及替换、查找字符获取索引、字符串提取、向其他类型转换、比较、判断字符串是否存在、分隔字符串、过滤空白字符串、大小写切换、判断是否以某个字符串开始或结束、提取字符串、获取长度、

QString对象构造

- QString ( const QChar * unicode, int size )//使用QChar数组中的size长度个字符构造QString
- QString ( const QChar * unicode )//使用QChar数组构造QString,结尾以'\0'结束
- QString ( QChar ch )//使用一个QChar字符构造QString
- QString ( int size, QChar ch )//使用size个ch构造QString
- QString ( const QLatin1String & str )//使用**单字节编码**的str构造QString
- QString ( const QString & other )//使用其他QString引用构造新的QQString
- QString ( const char * str )//使用字符串常量构造QString
- QString ( const QByteArray & ba )//使用字节数组构造QString

追加字符串

QString s = "str";
s += "ing";//s = "string"
s.append(" ");//s = "string "类似于push_back
s.append("test");//向后追加:s = "string test"
s.prepend("This is ");//向前追加:s = "This is string Test"类似于push_front

也可以使用push_back往字符串末尾添加子串,能达到一样的作用

组合字符串

可以使用sprintf函数和arg函数

QString ss.sprintf("%s %.1f%%", "Value", 100.0);//s = "Value 100.0%
s = QString("%1 %2 %3 %4").arg("This").arg("is").arg("a").arg("test");//s = "This is a test"

插入及替换

插入使用insert,替换使用replace

QString& insert ( int position, const QString & str )
QString& insert ( int position, const QLatin1String & str )
QString& insert ( int position, const QChar * unicode, int size )
QString& insert ( int position, QChar ch )

参数position表示插入的位置,从0开始计算
其他参数表示待插入的类型,在构造函数QString时有讲

QString&    replace ( int position, int n, const QString & after )
QString &   replace ( int position, int n, const QChar * unicode, int size )
QString &   replace ( int position, int n, QChar after )//替换成n个QChar

参数position同样表示开始替换的位置
参数n表示替换的长度
其他参数表示待替换的类型,在构造QString时有讲
在Qt的帮助文档中还提供了另外的几组replace函数,使用时可查文档

查找字符串获取第一次出现位置索引

使用indexof函数获取索引

int indexOf ( const QString & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
int indexOf ( const QLatin1String & str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
int indexOf ( QChar ch, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
int indexOf ( const QRegExp & rx, int from = 0 ) const
int indexOf ( QRegExp & rx, int from = 0 ) const

函数的作用是返回待查找字符串第一次出现的位置,没有找到目标字符串返回-1。
str表示待查找的各种形式表示的字符或字符串
QRegExp为通过正则表达式匹配的规则查找
from表示开始查找的位置,如果from = -1表示从最后一个字符开始,如果from = -2表示从倒数第二个开始,以此类推。
Qt::CaseSensitive表示区分大小写
Qt::CaseInsensitive不区分大小写

QString str = "the minimum";//官方例子
str.indexOf(QRegExp("m[aeiou]"), 0);       // returns 4

QString x = "std::string & QString";
QString y = "ing";
x.indexOf(y);               // returns 8
x.indexOf(y, 1);            // returns 8
x.indexOf(y, 10);           // returns 18
x.indexOf(y, 19);           // returns -1

清除指定子串

QString &   remove ( QChar ch, Qt::CaseSensitivity cs = Qt::CaseSensitive )
QString &   remove ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive )

QString s = "Hello World";
QString ss = s.remove("l");//ss = "Heo Word"

在母串中删除指定子串或字符

字符串提取

使用mid函数进行提取

//从position开始截取n个字符并返回
QString mid ( int position, int n = -1 ) const
//不使用默认参数
QString s = "QString";
QString ss = s.mid(13);//ss = "Str"

//省略第二个参数表示从position开始截取到末尾
QString s = "QString";
QString ss = s.mid(1);//ss = "String"

使用left和right函数提取

QString left(int n)const
QString right(int n)const

QString s = "QString";
QString ss = s.left(4);//ss = "QStr"

QString s = "QString";
QString ss = s.right(3);//ss = "ing"

函数分别表示从最左或最后提取n个长度的字符串并返回

从字符串到其他类型

使用的是toInt(), toLongLong(), toDouble()…等等。

QString str = "12";
int i = str.toInt();//i = 12

其他转换函数使用方法一致。

数字到字符串

静态函数number或者setNumber

QString s = QString::number(100.0);//参数包含int double long等等
QString ss;
ss.setNum(100.0);

比较字符串

包括不等于、等于、大于、小于、大于等于和小于等于

bool    operator!= ( const QString & other ) const
bool    operator< ( const QString & other ) const
bool    operator<= ( const QString & other ) const
bool    operator== ( const QString & other ) const
bool    operator> ( const QString & other ) const
bool    operator>= ( const QString & other ) const

参数表示被比较的字符串
其他重载版本查看帮助文档

判断字符串是否存在

contains ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const

判断字符串是否是empty

bool    isEmpty () const//原型

QString().isEmpty();            // returns true
QString("").isEmpty();          // returns true
QString("x").isEmpty();         // returns false
QString("abc").isEmpty();       // returns false

它判断字符串是不是空串,等价于if(QString(” “).length() == 0)

判断字符串是否NULL

bool    isNull () const//原型

QString().isNull();             // returns true
QString("").isNull();           // returns false
QString("abc").isNull();        // returns false

只有当构造QString不带任何参数时返回true

分隔字符串

通过使用QString::split()能够将一个大串按照某个子串划分成多个子串并存到QStringList中。

QString str = "You,I,She";
QStringList list= str.split(",");

结果list.at(0) = “You” list.at(1) = “I” list.at(2) = “She”

过滤空白字符串

QString trimmed () const//原型

QString s(" abc def ghi ");
s = s.trimmed();//s = "abc def ghi"

通过调用该函数可以去除首尾的空格

大小写切换

切换为全大写,使用toUpper()函数

QString s = "Hello World";
QString ss = s.toUpper();

切换为全小写,使用toLower()函数

QString s = "Hello World";
QString ss = s.toLower();

判断是否以某个字符串开始或结束

(1)以某子串开始,使用startsWith()函数

QString s = "http:www.baidu.com";
bool i = s.startsWith("http:");

结果为i = true;

(2)以某子串结束,使用endsWith()函数

QString str = "http:www.baidu.com";
bool i = str.endsWith("com");

结果为i = true;

获取子串出现的次数

//原型
int QString::count ( const QString & str, Qt::CaseSensitivity cs = Qt::CaseSensitive ) const
//示例
QString s = "Hello World";
qDebug() << s.count("l");// 输出3

结果返回的是str在母串中出现的次数

获取字符串长度

int QString::length () const//原型

//示例

QString s = "Hello World";
qDebug() << s.length();//输出11,说明不带'\0'
  • 11
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值