Results 1 to 5 of 5

Thread: Get Perticular String from QString

  1. #1
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Question Get Perticular String from QString

    I have a String like below
    Qt Code:
    1. QString mystring = "0001f0 9f 80 a2 06 bd 40 23 5f 16 8c d8 37 43 fb b4 a4 .....@#_...7C... "
    2. QStringList list = mystring .split(" ");// not working as per expectation
    To copy to clipboard, switch view to plain text mode 
    I want to break the string in such a way that my output should be like below
    Output
    0001f0
    9f 80 2a 06 bd 40 23 5f
    16 8c d8 37 43 fb b4 a4

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get Perticular String from QString

    What does it mean not working as per expectation ?

  3. #3
    Join Date
    Oct 2014
    Posts
    71
    Thanks
    13
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Perticular String from QString

    I am getting the output as below
    list[0] = 0001f0
    list[1] = 9f
    list[2] = 80
    list[3] = 2a
    list[4] = 06
    list[5] = bd
    list[6] = 40
    list[7] = 23
    list[8] = 5f
    list[9] = 16
    list[10] = 8c
    list[11] = d8
    list[12] = 37
    list[13] = 43
    list[14] = fb
    list[15] = b4
    list[16] = a4
    but my output should be like below
    list[0] = 0001f0
    list[1] = 9f 80 2a 06 bd 40 23 5f
    list[2] = 16 8c d8 37 43 fb b4 a4

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Get Perticular String from QString

    You ask to split with a space so what are you surprised ?
    Is this string has a fixed format ? If yes just use QString::left, QString::mid and QString::right methods.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get Perticular String from QString

    Something like this should work, if your lines are always formatted as you show (a 6-byte hex address first, followed by hex as 8 two-byte groups):

    Qt Code:
    1. QRegularExpression regExp( "^[0-9a-fA-F]6\\s+|(([0-9a-fA-F]2\\s+)8)+");
    2. string.split( regExp, QString::SkipEmptyParts );
    To copy to clipboard, switch view to plain text mode 

    Untested, you might have to play around with the regular expression to get it working the way you want. You will probably also have to trim whitespace from the ends of the sub-strings because the regular expression includes the separators as part of the matching.
    Last edited by d_stranz; 1st April 2016 at 17:27.

Similar Threads

  1. Replies: 8
    Last Post: 25th November 2010, 11:40
  2. string (pascal) to QString QT? how
    By triperzonak in forum Newbie
    Replies: 0
    Last Post: 24th November 2008, 04:06
  3. Qstring vs string
    By cbarmpar in forum Qt Programming
    Replies: 1
    Last Post: 31st August 2008, 21:23
  4. std::string to QString
    By baray98 in forum Qt Programming
    Replies: 1
    Last Post: 19th July 2008, 03:56
  5. std::string to QString?
    By Backslash in forum Newbie
    Replies: 4
    Last Post: 1st August 2007, 15:50

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.