Results 1 to 5 of 5

Thread: QString split()

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QString split()

    If I have a string that's delimited by 2 different delimiters, how can I use split() or something similar to split the string?

    i.e.

    I want to split this sentence:

    $GPGGA,17.4,233.3*22 // delimiters are , and *

    into

    $GPGGA
    17.4
    233.3
    22

  2. #2
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString split()

    Quote Originally Posted by ShaChris23 View Post
    If I have a string that's delimited by 2 different delimiters, how can I use split() or something similar to split the string?

    i.e.

    I want to split this sentence:

    $GPGGA,17.4,233.3*22 // delimiters are , and *

    into

    $GPGGA
    17.4
    233.3
    22
    What about using a regular expression something like this
    Qt Code:
    1. QRegExp rx("(\\,|\\*)");
    2. QString str = "$GPGGA, 17.4, 233.3 * 22";
    3. int pos = 0;
    4.  
    5. while ((pos = rx.indexIn(str, pos)) != -1) {
    6. qDebug( rx.cap(1).toLatin1().data() );
    7. list << rx.cap(1);
    8. pos += rx.matchedLength();
    9. }
    To copy to clipboard, switch view to plain text mode 

    The above code gives the position of ',' and '*'. You can play around to get the exact string

  3. The following user says thank you to vermarajeev for this useful post:

    ShaChris23 (2nd May 2007)

  4. #3
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QString split()

    Checkout QString::replace() to ensure you have only one separator,
    then use QString::split() to divide the string by that separator.

  5. The following user says thank you to drhex for this useful post:

    ShaChris23 (2nd May 2007)

  6. #4
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString split()

    Here's how I did it in straight C++ with STL before. Can I do something similar in Qt?

    Qt Code:
    1. void ParseSentence( string& mSentence,
    2. string& mDelimiters,
    3. vector<string>& mFields )
    4. {
    5. // Skip delimiters at beginning.
    6. string::size_type lastPos = mSentence.find_first_not_of( mDelimiters, 0);
    7. // Find first "non-delimiter".
    8. string::size_type pos = mSentence.find_first_of( mDelimiters, lastPos);
    9.  
    10. while (string::npos != pos || string::npos != lastPos)
    11. {
    12. // Found a token, add it to the vector.
    13. mFields.push_back( mSentence.substr( lastPos, pos - lastPos ) );
    14. // Skip delimiters. Note the "not_of"
    15. lastPos = mSentence.find_first_not_of( mDelimiters, pos );
    16. // Find next "non-delimiter"
    17. pos = mSentence.find_first_of( mDelimiters, lastPos );
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. example of usage:
    2. // Parse sentence
    3. ParseSentence( str,
    4. string(", *"),
    5. str_fields );
    To copy to clipboard, switch view to plain text mode 

  7. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QString split()

    If you are happy with above code then use it directly, Qt will not complain. But take care to include appropriate header files for your STL classes. Once you are happy with what you got after parsing, then QString (a Qt string ) takes std::string as argument in one of its constructor.

    Njoy

  8. The following user says thank you to vermarajeev for this useful post:

    ShaChris23 (3rd May 2007)

Similar Threads

  1. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  2. Convert from iso-8859-1 to... Something else :-)
    By Nyphel in forum Qt Programming
    Replies: 4
    Last Post: 7th March 2007, 17:59
  3. QString problem
    By vermarajeev in forum Qt Programming
    Replies: 9
    Last Post: 26th October 2006, 19:10
  4. QSqlQueryModel + set Write
    By raphaelf in forum Qt Programming
    Replies: 7
    Last Post: 5th June 2006, 08:55
  5. Converting QString to unsigned char
    By salston in forum Qt Programming
    Replies: 3
    Last Post: 24th April 2006, 22:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.