zoukankan      html  css  js  c++  java
  • 【qt】【QString的诸多操作】

    前言:

      qt的数据处理莫过于QString,QString对于字符串的操作多的数不胜数。下面博主就将常用的罗列出来,一起分享。

    正文:

      下面的操作具体为:追加,查找,删除,提取,分割,各种转换等等。

    0.字符串长度:length

      QString str = "hello world";

      int strNum = str.length();//11

    1.追加字符串:+

      QString的追加字符串比较简单。可以直接2个字符串用"+"连接即可。

      QString str1 = “hello”;

      QString str2 = “world”;

      QString str = str1 + str2;// helloworld;

    2.查找字符串:indexOf

      indexOf();查找到字串返回字串第一次出现的下标,否则返回-1;

      QString str = “helloworld”;

      int strNum = str.indexOf("wor");//5

    3.删除字符串:remove

      将指定字符串从父母串中删除;

      QString str = "hello word";

      QString str2 = str.remove("o wo");//hellrd

    4.字符串提取:mid

      4.1.提取指定位置的n个字符串。

      QString str = ”hello word“;

      QString str2 = str.mid(1,3);//ell

      4.2.提取指定位置到末尾的字符串。

      QString str = "hello world";

      QString str2 = str.mid("2");//llo world

      4.3.提取开头到指定位置的字符串。

      QString str = "hello world";

      QString str2 = str.left(3);//hell

      4.4.提取指定位置到末尾的字符串。

      QString str = "hello world";

      QString str2 = str.right(3);//lo world

    5.分割字符串:section

      将字符串依照某个字符分割成若干快,进行提取。

      QString str = "1,2,3,4,5,6";

      QString str2 = str.section(",", 1,1).trimmed();//2,,第二个参数是起始分隔符,第三个参数是到第几个分割符。

    6.字符串转数字:toInt

      QString str = "111";

      int a = 1 + str.toInt();//112

    7.字符串换数组:QByteArray

      QByteArray byteArray = QString.toLatin1();  // 这种方法遇到中文会变成????。
           QByteArray byteArray = QString.toStdString().data();    //这种方法有可能会给不到数组大小,需要先求大小再循环。

    8.字符串转bool:
      

     bool testParam;
        QString tempParam = QString::number(testParam);    
        //上面是bool 转QString;

        QVariant tempValue = tempParam;
        bool tempFinished = tempValue.toBool();
        //这样就把QString 类型的转换回去了。

    9.qint8转QString:    

    qint8 a = 9;    

    QString b=tr("%1").arg(a);

    10.Qstring到string的转化

    //从QString 到 std::string    

    str = qstr.toStdString();

    //从std::string 到QString    

    qstr = QString::fromStdString(str);

    11.unsigned char 转化QString:  

    unsigned char buf[]="<ocs><header t="login" c="1" i="-1" /><body><u>";   

    string bufs = (char*)buf;   

    QString bufq = QString::fromStdString(bufs);   

    ui->lineEdit->setText(bufq);

    12.QString 转char*

            QString ss = "<ocs><header t="login" c="1" i="-1" /><body><u><ocs><header t="login" c="1" i="-1" /><body><u><ocs><header t="login" c="1" i="-1" /><body><u>";   

       char*  cc = ss.toUtf8().data();   

       int len = ss.size();   

      cout << len<< endl;   

      for(int i=0; i<len; i++)    {

              cout << cc[i];   

       }

    13.过滤末尾和开头的空白字符串:trimmed

      QString str(" abc def ghi ");

      str = str.trimmed();//str = "abc def ghi"

    14.字符串大小写切换:toUpper

      QString s = "Hello World";

      QString ss = s.toUpper();//HELLO WORLD

      QString s = "Hello World";

      QString ss = s.toLower();//hello world

     

    15.判读字符串的开头和结尾:

    QString s = "http:www.baidu.com";

    bool i = s.startsWith("http:");//true

    QString str = "http:www.baidu.com";

    bool i = str.endsWith("com");//true

     

    16.判断子串出现的次数:

      QString str = "Hello World";

      QString str2 = str.count("l");// 输出3

     

     

     

    后记:

      好多,好多。。以后遇到还会补充到评论区,也欢迎大家评论区补充哦。

     

     

     

     

     

     

     

     

     

      

  • 相关阅读:
    leetcode701. Insert into a Binary Search Tree
    leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
    leetcode 110. Balanced Binary Tree
    leetcode 104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree
    二叉树
    leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
    5. Longest Palindromic Substring
    128. Longest Consecutive Sequence
    Mac OS下Android Studio的Java not found问题,androidfound
    安卓 AsyncHttpClient
  • 原文地址:https://www.cnblogs.com/13373-/p/11468904.html
Copyright © 2011-2022 走看看