zoukankan      html  css  js  c++  java
  • Qt基础1:QString

    头文件:#include <QString>

    1、拼接

      1)直接使用“+”;

      2)append() 在字符串后面添加字符串;

      3)prepend() 在字符串前面添加字符串。

      QString str1 = "nihao ";
      QString str2 = "shijie";
      QString str3 = "";
      str3 = str1 + str2;  //nihao shijie
      str1.append(str2);  //nihao shijie
      str1.prepend(str2);  //"shijienihao "

    2、求字符串长度

      count(),size(),length()都能返回字符串长度,

      注:如果字符串中含有汉字,一个汉字算一个字符。

    3、大小写转换

      toUpper() 转换为大写  //str1.toUpper();

      tolower() 转换为小写  //str2.toLower();

    4、去除字符串中的空格

      trimmed()   去除字符串首尾的空格

      simplified()  去除字符串的首尾空格;并且将中间的连续空格换为一个空格代替。

      补充: 去除字符串中的多有字符(结合正则表达式)

        str.remove( QRegExp("\s") );

    5、在字符串中查找参数字符串出现的位置

      indexOf()  在字符串中查找参数字符串出现的位置

        最多指定三个参数:

        1、参数字符串;  2、开始匹配位置;  3、指定是否区分大小写

      lastIndexOf()  参数字符串最后出现的位置

    6、判断字符串是否为空

      isNull()  和  isEmpty()

      QString str1,QString str2=“”;

      str1.isNull()  //返回true  //为赋值字符串

      str2.isNull()  //返回false  //只有“”的字符串,也不是Null

      str1.isEmpty()  //返回 true

      str2.isEmpty()  //返回 true

      注:QString只要赋值就会在最后加上“”,因此,在实际使用中常用isEmpty();

    7、判断字符串中是否包含某个字符串

      contains()

      第一个参数指定字符串,第二个参数指定是否区分大小写。

    8、其他方法

        str1.endsWith("cloos",Qt::CaseInsensitive);     //判断字符串是否以某个字符串结尾
        str1.startsWith("cloos",Qt::CaseSensitive);     //判断字符串是否以某个字符串开始
    
        str2 = str1.left(3);        //返回从字符串左边取3个字符
        str2 = str1.right(3);       //返回从字符串右边取3个字符
        str2 = str1.mid(3,4);       //返回从字符串左边第三个字符开始取4个字符。
    
        str2 = str1.section(",",0,3);   //从字符串中提取以第一个参数作为分隔符,从0端开始到3端的字符串。

     9、与数值之间的转换

      1)转换为整数

        int toInt(bool *ok = Q_NULLPTR, int base=10)  const

        long toLong(bool *ok = Q_NULLPTR, int base=10)  const

        short toShort(bool *ok = Q_NULLPTR, int base=10)  const

        uint toUInt(bool *ok = Q_NULLPTR, int base=10)  const

        ulong toULong(bool *ok = Q_NULLPTR, int base=10)  const

        使用方法:

          str.toInt();  第一个参数返回转换是否成功;第二个参数指定进制,默认为10进制。

      2)转换为浮点数

        double toDouble(bool *ok = Q_NULLPTR)  const

        float toFloat(bool *ok = Q_NULLPTR)  const

      3)数值转换为QString

        以显示两位小数为例

        str = QString::number(num,'f',2);

        str = QString::asprintf( "%.2",num);

        str = str.setNum(num,"f",2);

        str = str.sprintf( "%.2", num);

  • 相关阅读:
    HDU 4778 内存搜索&amp;如压力
    cocos2dx-2.x CCFileUtils文件管理分析(2)
    开源 自由 java CMS
    1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup
    什么是PV,UV。
    Python爬虫框架Scrapy获得定向打击批量招聘信息
    采用ToolRunner执行Hadoop基本面分析程序
    编辑时snapping的添加
    利用ArcGIS Engine、VS .NET和Windows控件开发GIS应用
    由图层判断数据源类型
  • 原文地址:https://www.cnblogs.com/longxi/p/10795826.html
Copyright © 2011-2022 走看看