Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to check is there is a string that I want?
Forum Updated to NodeBB v4.3 + New Features

How to check is there is a string that I want?

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    victor wang
    wrote on last edited by
    #1

    Hi All,
    I'm using qt5.5 on my computer.
    I wanna know is there a string that I want in a QString.
    After reading some document, I'm using indexof() to do it.

    I'm using this to get my QString.

    QString wifi_ping = "ping -I eth0 www.google.com.tw -c 1";
    Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<wifi_ping,&wifi_message);
    

    This is what I got in my QString.

    PING 192.168.120.112 (192.168.120.112) from 192.168.120.115 eth0: 56(84) bytes of data.
    64 bytes from 192.168.120.112: icmp_seq=1 ttl=64 time=0.321 ms
    
    --- 192.168.120.112 ping statistics ---
    1 packets transmitted, 1 received, 0% packet loss, time 0ms
    rtt min/avg/max/mdev = 0.321/0.321/0.321/0.000 ms
    

    And I check it in my program like below.

    if(wifi_message.indexof("bad"))
    {
         qDebug("Failed");
    }
    else if(wifi_message.indexof("ping statistics"))
    {
         qDebug("pass");
    }
    

    Unfortunately, It will always goes Failed.
    And i check the document, I am sure there is no "bad" string.
    Why it will show Failed every time?

    Thanks in Advance!

    joeQJ raven-worxR 2 Replies Last reply
    0
    • V victor wang

      Hi All,
      I'm using qt5.5 on my computer.
      I wanna know is there a string that I want in a QString.
      After reading some document, I'm using indexof() to do it.

      I'm using this to get my QString.

      QString wifi_ping = "ping -I eth0 www.google.com.tw -c 1";
      Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<wifi_ping,&wifi_message);
      

      This is what I got in my QString.

      PING 192.168.120.112 (192.168.120.112) from 192.168.120.115 eth0: 56(84) bytes of data.
      64 bytes from 192.168.120.112: icmp_seq=1 ttl=64 time=0.321 ms
      
      --- 192.168.120.112 ping statistics ---
      1 packets transmitted, 1 received, 0% packet loss, time 0ms
      rtt min/avg/max/mdev = 0.321/0.321/0.321/0.000 ms
      

      And I check it in my program like below.

      if(wifi_message.indexof("bad"))
      {
           qDebug("Failed");
      }
      else if(wifi_message.indexof("ping statistics"))
      {
           qDebug("pass");
      }
      

      Unfortunately, It will always goes Failed.
      And i check the document, I am sure there is no "bad" string.
      Why it will show Failed every time?

      Thanks in Advance!

      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by joeQ
      #2

      @victor-wang hi,friend,welcome.

      int QString::indexOf(const QString &str, int from = 0, Qt::CaseSensitivity cs = Qt::CaseSensitive) const
      Returns the index position of the first occurrence of the string str in this string, searching forward from index position from. Returns -1 if str is not found.
      If cs is Qt::CaseSensitive (default), the search is case sensitive; otherwise the search is case insensitive.
      If from is -1, the search starts at the last character; if it is -2, at the next to last character and so on.

      qDebug() << wifi_message; ///< to see the string content
      ///< you can to putout the result
      //qDebug() << wifi_message.indexof("bad");
      if(0 <= wifi_message.indexof("bad")){
           qDebug("Failed");
      }else if(0 <= wifi_message.indexof("ping statistics")){
           qDebug("pass");
      }
      

      try again.

      Just do it!

      1 Reply Last reply
      0
      • V victor wang

        Hi All,
        I'm using qt5.5 on my computer.
        I wanna know is there a string that I want in a QString.
        After reading some document, I'm using indexof() to do it.

        I'm using this to get my QString.

        QString wifi_ping = "ping -I eth0 www.google.com.tw -c 1";
        Process::executeProcessSync(QString("sh"),QStringList()<<"-c"<<wifi_ping,&wifi_message);
        

        This is what I got in my QString.

        PING 192.168.120.112 (192.168.120.112) from 192.168.120.115 eth0: 56(84) bytes of data.
        64 bytes from 192.168.120.112: icmp_seq=1 ttl=64 time=0.321 ms
        
        --- 192.168.120.112 ping statistics ---
        1 packets transmitted, 1 received, 0% packet loss, time 0ms
        rtt min/avg/max/mdev = 0.321/0.321/0.321/0.000 ms
        

        And I check it in my program like below.

        if(wifi_message.indexof("bad"))
        {
             qDebug("Failed");
        }
        else if(wifi_message.indexof("ping statistics"))
        {
             qDebug("pass");
        }
        

        Unfortunately, It will always goes Failed.
        And i check the document, I am sure there is no "bad" string.
        Why it will show Failed every time?

        Thanks in Advance!

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by raven-worx
        #3

        @victor-wang said in How to check is there is a string that I want?:

        if(wifi_message.indexof("bad"))
        {
        qDebug("Failed");
        }
        else if(wifi_message.indexof("ping statistics"))
        {
        qDebug("pass");
        }

        your checks are not correct (false-positive)!
        In case it doesn't find the string "bad" -1 is returned. In C++ everything that is not equal zero is true when implicitly converted to bool.

        So your checks should rather look like this:

        if(wifi_message.indexof("bad") >= 0)
        {
             qDebug("Failed");
        }
        else if(wifi_message.indexof("ping statistics") >= 0)
        {
             qDebug("pass");
        }
        

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        V 2 Replies Last reply
        3
        • raven-worxR raven-worx

          @victor-wang said in How to check is there is a string that I want?:

          if(wifi_message.indexof("bad"))
          {
          qDebug("Failed");
          }
          else if(wifi_message.indexof("ping statistics"))
          {
          qDebug("pass");
          }

          your checks are not correct (false-positive)!
          In case it doesn't find the string "bad" -1 is returned. In C++ everything that is not equal zero is true when implicitly converted to bool.

          So your checks should rather look like this:

          if(wifi_message.indexof("bad") >= 0)
          {
               qDebug("Failed");
          }
          else if(wifi_message.indexof("ping statistics") >= 0)
          {
               qDebug("pass");
          }
          
          V Offline
          V Offline
          victor wang
          wrote on last edited by
          #4

          @joeQ
          Thanks it works now!

          1 Reply Last reply
          0
          • raven-worxR raven-worx

            @victor-wang said in How to check is there is a string that I want?:

            if(wifi_message.indexof("bad"))
            {
            qDebug("Failed");
            }
            else if(wifi_message.indexof("ping statistics"))
            {
            qDebug("pass");
            }

            your checks are not correct (false-positive)!
            In case it doesn't find the string "bad" -1 is returned. In C++ everything that is not equal zero is true when implicitly converted to bool.

            So your checks should rather look like this:

            if(wifi_message.indexof("bad") >= 0)
            {
                 qDebug("Failed");
            }
            else if(wifi_message.indexof("ping statistics") >= 0)
            {
                 qDebug("pass");
            }
            
            V Offline
            V Offline
            victor wang
            wrote on last edited by
            #5

            @raven-worx
            Thanks it works now!

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved