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 if a string is valid?
Forum Updated to NodeBB v4.3 + New Features

How to check if a string is valid?

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 4.3k 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.
  • A Offline
    A Offline
    amahta
    wrote on last edited by
    #1

    I have to process a text file which contains of many lines with a string pattern like this:
    (Number)(Whitespace)(Number)(Whitespace)(Number)(Whitespace)(Number)(Whitespace)
    Exclude "(" characters.
    I tried to do this using exceptions but I notices that it does not work.
    Is there any other way to do this?

    Thou shalt programme
    http://www.amin-ahmadi.com

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kqt-mr-nv6
      wrote on last edited by
      #2

      collect chunks of data into QString
      and use 'split' method with white space as 'separator'
      then you can use 'toint' method to check for valid
      numbers

      repeat till the end of file

      1 Reply Last reply
      0
      • A Offline
        A Offline
        amahta
        wrote on last edited by
        #3

        Actually I'm exactly doing that right now.
        @
        someVector.push_back(oscStrings[i].split(" ", QString::SkipEmptyParts)[0].toDouble());
        @
        As long as the pattern is the same no problems occur, but if it's a wrong file then my app crashes :(

        Thou shalt programme
        http://www.amin-ahmadi.com

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          Maybe first use the readLine from the file, check if there are any whitespaces with count(' '); function. Then do the split/join stuff.
          Also, chop up the push_back argument to determine the cause of the crash! Not it's just guessing. Maybe it is the toDouble, maybe the split, maybe the SkipEmptyParts, who knows???
          Never be afraid of using local variables to keep the code readable and manageable.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kqt-mr-nv6
            wrote on last edited by
            #5

            bq.
            @someVector.push_back(oscStrings[i].split(" ", QString::SkipEmptyParts)[0].toDouble());@

            is it crashing at this line ?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              amahta
              wrote on last edited by
              #6

              Thanks for the help guys.
              I solved it using the following code:
              @
              bool err;
              double dbl;
              if(oscStrings[i].split(" ", QString::SkipEmptyParts).count() != someNumber)
              // Handle error and return
              else
              dbl = someStr.split(" ", QString::SkipEmptyParts)[0].toDouble(&err);
              if(err)
              // Handle error and return
              _someVector.push_back(dbl);
              @

              But now I wonder if there is any kind of QString validator or something to do this easier than this??!!

              Thou shalt programme
              http://www.amin-ahmadi.com

              1 Reply Last reply
              0
              • JeroentjehomeJ Offline
                JeroentjehomeJ Offline
                Jeroentjehome
                wrote on last edited by
                #7

                You might want to use the QRegExp class for that if that is more appropriate for you.

                Greetz, Jeroen

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  amahta
                  wrote on last edited by
                  #8

                  I checked QRegExp but I wasn't able to make out how to define a whole bunch of whitespaces because I don't know what kind of whitespaces (space, tab etc.) will be in my text file. And also for double (or floating point numbers) I wasn't able to find a simple solution.
                  Jeorentje, do you know of any examples similar to what I want to do?

                  Thou shalt programme
                  http://www.amin-ahmadi.com

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    MarianMMX
                    wrote on last edited by
                    #9

                    bq. I checked QRegExp but I wasn’t able to make out how to define a whole bunch of whitespaces because I don’t know what kind of whitespaces (space, tab etc.) will be in my text file. And also for double (or floating point numbers) I wasn’t able to find a simple solution.bq.

                    @QString QString::simplified() const

                    Returns a string that has whitespace removed from the start and the end, and that has each sequence of internal whitespace replaced with a single space.@
                    http://qt-project.org/doc/qt-5.0/qtcore/qstring.html#simplified

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      amahta
                      wrote on last edited by
                      #10

                      Thanks MarianMMX. This solves half of my problem.
                      Now I only need to find a way to validate a double number using Qt's RegularExpression. Any ideas?

                      Thou shalt programme
                      http://www.amin-ahmadi.com

                      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