QString类常用函数

 

        QT的QString类是一个常用类,提供了很方便的对字符串操作的接口。

        最近老是用到这个类,就总结了一下QString类的常用函数,例:

        1、

                QString str1 = "hello world!";

 

                //获取字符串的长度

                str1.length();        //=5

                //从最左边开始截取两个字符

                str1.left(2);          //=he

                /从最右边开始截取两个字符

                str1.right(2);       //=d!

                //截取字符串,第一个参数是起始位置,第二个参数是长度

                str1.mid(3, 2);     //=lo

                //在字符串末尾添加字符

                str1.append("haha");        //=hello world!haha

        2、

                QString str2 = " hello , world ! ";        //有五个空格

 

                //去掉字符串首尾的空格

                str2.trimmed();           //=hello , world !

                //移除字符串中的某个字符

                str2.remove(" ");        //=hello,world!

        3、

                //格式化字符串输出,其中的%1等为占位符

                QString str3 = QString("%1 %2 (%3--%4)").arg("hello").arg("world").arg(100).arg(500);

                //str3 = hello world (100--500)

 

        4、分割字符串

                QString str = "a;b;c;";

                QStringList strList = str.split(";");

                QString result1 = strList.at(0);   //=a        

                QString result2 = strList.at(1);   //=b

                QString result3 = strList.at(2);   //=c

                QString result4 = strList.at(3);   //=""

 

        5、转换函数:

                ① toAscii():返回一个ASCII编码的8位字符串;                

                ② toLatin1():返回一个Latin-1(ISO8859-1)编码的8位字符串;

                ③ toUtf8():返回一个UTF-8编码的8位字符串(UTF-8是ASCII码的超级,它支持整个Unicode字符集);

              

                ④ toLocal8Bit():返回一个系统本地(locale)编码的8位字符串。

 

 

 

 

 

 

  • 4
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

画茧自缚

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值