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] How to sscanf integers from QString?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to sscanf integers from QString?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 4 Posters 6.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.
  • L Offline
    L Offline
    loudking
    wrote on last edited by
    #1

    Hello I have a QString = "2014-12-23-23-34-45-<random integer 10 digits>".

    The first six integers are year, month, day, hour, minute and second, while the last integer is a random one with 10 digits.

    Could anybody tell me how to do extract year, month, day, hour, minute and second value from this QString and store them into six different integers? Thanks in advance.

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      Why not use QDateTime and solve it in a single line?

      But, since you ask to have them separately, here you go:
      @
      QString string(“2014-12-23-23-34-45-<random integer 10 digits>”);
      int year, month, day, hour, minute, second;

      year = string.left(4).toInt();
      month = string.mid(5, 2).toInt();
      day = string.mid(8, 2).toInt();
      // ... and so on
      @

      (Z(:^

      1 Reply Last reply
      0
      • K Offline
        K Offline
        KushalAusula
        wrote on last edited by
        #3

        Is the size and format does not change any time?? and what about hyphen(-)??? will it be there in all packects of data ? if so

        if there is hyphen(which can be considered as delimiter), you can extract based on it's position.

        else you dont have hyphen, then you can extract based on the static positions of "The first six integers are year, month, day, hour, minute and second, while the last integer is a random one with 10 digits"

        1 Reply Last reply
        0
        • TheBadgerT Offline
          TheBadgerT Offline
          TheBadger
          wrote on last edited by
          #4

          A regular expression could also be used:
          @
          (\d{4})-(\d{1,2})-(\d{1,2})-(\d{1,2})-(\d{1,2})-(\d{1,2})-(\d{10})
          @

          Just make sure to escape it properly when passing as string to a "QRegularExpression":http://qt-project.org/doc/qt-5/qregularexpression.html (or if you have Qt 4 "QRegExp":http://qt-project.org/doc/qt-4.8/qregexp.html,

          With the regular expression supplied it will handle the case where say the month is jan and it is in the string as 1, not 01.


          Check out my SpellChecker Plugin for Qt Creator @ https://github.com/CJCombrink/SpellChecker-Plugin

          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