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. [SOLVED] Extract numbers from QString
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Extract numbers from QString

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 15.4k 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
    VVCephei
    wrote on last edited by
    #1

    Hey there,

    should be simple, but I'm very new to Qt and my brain is just overloaded with that huge amount of functions and classes and other Qt-stuff. :D

    I have a QString:

    @QString str("1 2 3 9 7 5 29 22 9f");@

    I want to extract all numbers from this QString, seperated by whitespace. See the "9f" in the end? This 9 should NOT be extracted, just numbers with whitespace before and after them. No extraction for you, f.
    The numbers should be pushed into a std::vector, for working with some library functions.

    Thank you,

    Ceriana

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      You could create a QStringList with QString::split() using the space as a separator. Then loop on the list using toInt(bool *ok) to retrieve the number value (which should fail for 9F since it would be hexadecimal).

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        use QRegExp and define a suitable pattern and search for it.

        Or use this:
        @
        QString str("1 2 3 9 7 5 29 22 9f");
        foreach( QString numStr, str.split(" ", QString::SkipEmptyParts) )
        {
        bool check = false;
        int val = numStr.toInt(&check, 10);
        if( !check )
        continue;
        else
        //do something with "val"
        }
        @

        Edit: again SGaist was 20 secs faster :P

        --- 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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MuldeR
          wrote on last edited by
          #4

          The following QRegExp should work to find all sequences of numeric characters (0-9) that are preceded by at least one whitespace and that are followed by at least one whitespace. We use it in a loop to find them all:

          @QRegExp rx("\s(\d+)\s");
          std::vector<int> result;
          int pos = 0;
          while ((pos = rx.indexIn(str, pos)) != -1)
          {
          QString theNumber = rx.cap(1);
          cout << """ << theNumber.toLatin1().constData() << """ << endl;
          result.push_back(theNumber.toInt());
          pos += rx.matchedLength();
          }@

          To handle also handle the special case where the number is located at the very beginning of the string (with no whitespace in front) or at the very end of the string (with no trailing whitespace), you'd need these too:

          @QRegExp rx_begin("^(\d+)\s");
          QRegExp rx_end("\s(\d+)$");@

          --

          Update:

          You probably could simplify all three cases to:

          @QRegExp rx("\b(\d+)\b");
          std::vector<int> result;
          int pos = 0;
          while ((pos = rx.indexIn(str, pos)) != -1)
          {
          QString theNumber = rx.cap(1);
          cout << """ << theNumber.toLatin1().constData() << """ << endl;
          result.push_back(theNumber.toInt());
          pos += rx.matchedLength();
          }@

          My OpenSource software at: http://muldersoft.com/

          Qt v4.8.6 MSVC 2013, static/shared: http://goo.gl/BXqhrS

          Go visit the coop: http://youtu.be/Jay...

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VVCephei
            wrote on last edited by
            #5

            I tested all three ways and got it to work as intended. Thank you guys :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Don't forget to update the thread's title to solved since all seems working now so other forum users may know a solution has been found :)

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              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