QString类的使用

QString的类保存16位的Unicode值,提供了丰富的操作,查询和转换等函数。该类还进行了隐式共享,高效的内存分配

策略等多方面优化。

 

一:

1-1.操作字符串:

 QString提供了一个二元的“+”操作符用于组合两个字符串

QString str1=" welcome ";
str1=str1+" to you ";             //str1= welcome to you;

QString str2="Hello ";
str2+=" World";                    //str2= Hello World;

 

1-2.使用的QString ::追加()函数添加字符串:

QString str1="Welcome ";
QString str2=" to ";
str1.append(str2);            //str1= Welcome to ;

str1.append(" you !");        //str1=Welcome to you !;

 

1-3.组合字符串的另一个函数是的QString::sprintf()

QString str;
str.sprintf("%s","Welcome ");                  //str=Welcome
str.sprintf("%s"," to you ! ");                //str=to you !
str.sprintf("%s %s","Welcome ","to you ! ");   //str=Welcome to you !

 

1-4.Qt还提供了另一种方便的字符组合方式,使用QString::arg()函数:

    //str=Jhon was born in 1998
    QString str1;
    str1=QString("%1 was born in %2.").arg("Jhon").arg(1998);

 

5.QString也提供了一些其他组合字符串的方法:

5-1:insert()函数:在原字符特定的位置插入一个字符串。

5-2:prepend()函数: 在原字符的开头插入一个字符串。

5-3:replace()函数: 在指定的字符串代替原字符串中的某些字符。

5-4:trimmed()函数: 移除字符串两端的空白字符。

 

二:

查询字符串数据,查询字符串数据有多种方式.

2-1:QString::startsWith()判断一个字符串是否以某个字符串开头。

    QString str="Welcome to you";
    str.startsWith("Welcome",Qt::CaseSensitive);    //返回true
    str.startsWith("you ",Qt::CaseSensitive);       //返回false

 

2-2:QString::contains()函数判断一个字符是否出现过.。


    QString str="Welcome to you";
    str.contains("Welcome",Qt::CaseSensitive);    //返回true

 

三:

字符串的转换,QString类提供丰富的函数转换,可以将一个字符串转换为数值类型或者其它的字符编码集。

3-1:QString::toInt()函数将字符串转换为整型数值,类似有toDouble()、toFloat()  、 toLong() 、toLongLong()。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值