我是靠谱客的博主 机灵玫瑰,这篇文章主要介绍QT_字符串相关操作_QString,现在分享给大家,希望可以做个参考。

QT_字符串相关操作_QString

1、操作字符串

①QString提供了“+=”操作符

复制代码
1
2
3
4
5
QString str1 = "Welcome"; str1 = str1 + "to you"; //str1="Welcome to you" QString str2 = "Hello"; str2 += "World"; //str2="Hello World"

②QString::append()具有与"+="相同的功能,是现在一个字符串末尾追加一个字符串

复制代码
1
2
3
4
5
QString str1 = "Dai"; QString str2 = "wudi"; str1.append("zp"); //str1="Daizp" str1.append(str2); //str1="Daizpwudi"

③QString::sprintf(),此函数支持的定义格式和C++中的函数sprintf()定义是一样的

复制代码
1
2
3
4
5
QString str; str.sprintf("%s","Dai"); //str = "Dai" str.sprintf("%s","zp"); //str = "Daizp" str.sprintf("%s","wudi"); //str = "Daizpwuidi"

④Qt提供了一种方便字符串组合的方式,使用QString::arg()函数,此函数的重载可以处理很多的数据类型,相比于QString::sprintf(),函数QString::arg()是一个比较好的解决方案,因为它类型安全,完全支持Unicode,并且允许改变"%n"参数的顺序

复制代码
1
2
3
4
QString str; str = QString("%1 was born in %2").arg("Daizp").arg(1999); //str = "Daizp was born in 1999"

⑤QString提供了一些其他组合字符串的方法

复制代码
1
2
3
4
insert(); //在原字符串特定的位置插入另一个字符串 prepend(); //在源字符串的开头插入另一串字符串 replace(); //用指定的字符串去替换原字符串中的某些字符

⑥去除空格、制表符等空白内容的函数

复制代码
1
2
3
QString::trimmed(); //移除字符串两端的空白字符 QString::simplified(); //移除字符串两端的空白字符,使用单个空格字符替换字符串中出现的空白字符

2、查询字符数据

①函数QString::startsWith() 判断一个字符串是否以某个字符串开头,此函数有两个参数,第一个是指定的字符串,第二个参数指定是否大小写敏感(默认情况下大小写是敏感的)

复制代码
1
2
3
4
5
QString str = "Welcome to you!"; qDebug() << str.startsWith("welcome",Qt::CaseSensitive); //返回false,区分大小写 qDebug() << str.startsWith("welcome",Qt::CaseInsensitive); //返回true,不分大小写 qDebug() << str.startsWith("you",Qt::CaseSensitive); //返回false

②函数QString::endsWith()与QString::startsWith() 类似,查询结尾是否为指定字符串
③QString::contains()判断一个指定的字符串是否出现过

复制代码
1
2
3
QString str = "Welcome to you"; str.contains("to",Qt::CaseSensitive); //返回true

3、字符串的转换

①将一个字符串转换为数值类型或者其他类的字符编码集

复制代码
1
2
3
4
5
6
QString::toInt() 将字符转换为整形数值,类似的还有toDouble(),toFloat()等等。 QString str = "125"; bool ok; int hex = str.toInt(&ok,16); //ok = true, hex = 293 int dec = str.toInt(&ok,10); //ok = true, dec = 125

②QString 提供的字符编码集的转换函数将会返回一个const char类型版本的QByteArray,QByteArray(const char)构造的一个对象,QByteArray类具有一个字节数组,既可以存储原始字节,也可以存储传统的以’’结尾的8位字符串,在Qt中QByteArray比使用const char* 更方便,且QByteArray也支持隐式共享,转换函数有以下几种
1.toAscii():返回一个ASCII编码的8位字符串
2.toLatin1():返回一个Latin-1编码的8位字符串
3.toUtf8():返回一个UTF-8编码的8位字符串(UTF-8是ASCII码的超集,它支持整个Unicode字符集)
4.toLocal8Bit():返回一个系统本地编码的8位字符串

复制代码
1
2
3
4
5
6
7
8
9
QString str = "Welcome to you!"; //初始化一个字符串对象 QByteArray ba = str.toAscii(); // (a) qDebug() << ba ; // (b) ba.append("Hello,World!"); // (c) qDebug() << ba.data(); //输出最后结果 //(a)通过此函数将Unicode编码的字符串转换为ASCII码的字符串,存储在ba中 //(b)输出转换后的字符串 //(c)使用append对字符串追加字符串

4、NULL字符串和空(empty)字符串的区别
一个NULL字符串就是使用QString的默认构造函数或者使用(const char*)0 作为参数的构造函数创建的QString的字符串对象;而字符串就是一个大小为0的字符串,一个null字符串一定是空字符串,但是空字符串不一定是null字符串

复制代码
1
2
3
4
5
QString().isNull(); //结果为true QString().isEmpty(); //结果为true QString("").isNull(); //结果为false QString("").isEmpty(); //结果为true

最后

以上就是机灵玫瑰最近收集整理的关于QT_字符串相关操作_QString的全部内容,更多相关QT_字符串相关操作_QString内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(78)

评论列表共有 0 条评论

立即
投稿
返回
顶部