Results 1 to 2 of 2

Thread: Splitting QString

  1. #1
    Join Date
    Apr 2011
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows Maemo/MeeGo

    Question Splitting QString

    I have following QString:

    aaa="7\n+1000+1500\n+50-300\n-300+200\n8\n"

    Now I want to split string to following way:
    bb to be one line of text. (first is "7" second is "+1000+1500")

    and then if line start with + or - I need split bb to parts
    (first is a1=what ever a2=what ever
    second line a1="+1000" a2="+1500"
    and third line a1="+50" a2="-300" )

    I condsider Following code, but I face difficult it separator can be + or -

    while (i >= aaa.lenght()) {
    needed coode to parse first line to qstring bb (separator is line feed)
    i=i+bb.lenght()+1;
    if (bb="7") { Fine do something}
    else if (bb="7") { Fine do something}
    else if (bb.left(1)=="+" || bb.left(1)=="-") {
    needed coode to parse split bb to qstrings a1 and a2
    }
    }

    I'm very newbie of c++ so don't give too difficult theoretic answers

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Splitting QString

    You can split a QString using QString::split and use QRegExp for capture part of string matching a pattern

    Qt Code:
    1. // Split in lines
    2. QStringList lines = aaa.split('\n');
    3. QRegExp re("([+-]\\d*)([+-]\\d*)");
    4.  
    5. Q_FOREACH (QString line, lines) {
    6. if (re.exactMatch(line)) {
    7. a1 = rx.cap (1);
    8. a2 = rx.cap (2);
    9. // do something with a1 and a2
    10. }
    11. else {
    12. bb = line;
    13. // do something with bb
    14. }
    15. }
    To copy to clipboard, switch view to plain text mode 
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. Replies: 8
    Last Post: 25th November 2010, 11:40
  2. Replies: 4
    Last Post: 1st February 2010, 14:21
  3. Replies: 4
    Last Post: 31st January 2008, 20:44
  4. Splitting data so it fits into datagrams
    By toratora in forum Qt Programming
    Replies: 4
    Last Post: 27th April 2007, 18:01
  5. Splitting Translation Files
    By Jimmy2775 in forum Qt Programming
    Replies: 9
    Last Post: 3rd February 2006, 19:23

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.