我的主页

快递查询 :http://m.kuaidi100.com     爱快递查询 :http://www.aikuaidi.cn/              

http://www.qtcn.org/bbs/u/168050  [收藏] [复制]

w642833823

  • 10

    关注

  • 67

    粉丝

  • 42

    访客

  • 等级:新手上路
  • 总积分:43
  • 保密,2011-12-07

最后登录:2024-04-26

更多资料

日志

QString收藏

2020-05-15 13:13

  • QString 转C++自带标准string

    思路:
    使用QString类自带的toStdStirng()函数即可


例子std::string str;QString qStr = "两袖清风";str =qStr.toStdString();//c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同.qDebug()<<str.c_str()<<endl;
  • 1
  • 2
  • 3
  • 4
  • 5


  • string 转QString
    思路:

使用QString自带的静态构造函数 fromStdString(string &s)即可

例子std::string str="两袖清风";QString qStr=QString::fromStdString(str);qDebug()<<qStr.toUtf8().data()<<endl;头文件:#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  //只有“\0”的字符串,也不是Null

  str1.isEmpty()  //返回 true

  str2.isEmpty()  //返回 true
  注:QString只要赋值就会在最后加上“\0”,因此,在实际使用中常用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);

分类:QT|回复:0|浏览:363|全站可见|转载
 

Powered by phpwind v8.7 Certificate Copyright Time now is:04-26 19:36
©2005-2016 QTCN开发网 版权所有 Gzip disabled