How to check if a string is valid?



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



  • 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



  • 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 :(



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



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

    is it crashing at this line ?



  • 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??!!



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



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



  • 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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.