Results 1 to 6 of 6

Thread: How to count numbers in the end of QString

  1. #1
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to count numbers in the end of QString

    Hello again, sorry for beginners question but I need to count numbers in the end of QString. The format of QString is different like B9_02 (must get result of 2 from here) or TP145 (and 3 from here).
    What function should i use to get the count of numbers?

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to count numbers in the end of QString

    Start from the back of the string and check each (Q)char if it is a number (http://doc.qt.io/qt-4.8/QChar#isNumber). If it is, add 1 to a count.

    the first non-number that you find (or start of string), then stop the count.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

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

    naptizaN (17th July 2012)

  4. #3
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: How to count numbers in the end of QString

    QChar::isDigit

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

    naptizaN (17th July 2012)

  6. #4
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to count numbers in the end of QString

    ah yes, isDigit is better than isNumber in this case.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #5
    Join Date
    Jul 2012
    Posts
    14
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to count numbers in the end of QString

    Trying such a code:
    Qt Code:
    1. QString p=le2->text();
    2. int count = 0;
    3. for(int j = p.length(); j > 0; j--)
    4. {
    5. if (p.at(j).isDigit()==true)
    6. count++;
    7. if (p.at(j).isDigit()==false)
    8. break;
    9. }
    10. qDebug() << "count" << count;
    To copy to clipboard, switch view to plain text mode 
    But get such an error: ASSERT: "i >= 0 && i < size()" in file ../../../../Qt/Desktop/Qt/473/gcc/include/QtCore/qstring.h, line 702
    Can you help me and write what i am doing wrong? using p[j] gives no error but gives no result (count =0 in any case).
    Last edited by naptizaN; 18th July 2012 at 07:35.

  8. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to count numbers in the end of QString

    If the string length is 5 then the character indexes are 0 through 4. Try starting with character length() - 1.

  9. The following user says thank you to ChrisW67 for this useful post:

    naptizaN (18th July 2012)

Similar Threads

  1. How to display the numbers in a QList<QString> ?
    By harish in forum Qt Programming
    Replies: 4
    Last Post: 3rd January 2012, 04:26
  2. Replies: 2
    Last Post: 11th August 2011, 15:42
  3. how to count total numbers items in listwidget
    By trupti in forum Qt Programming
    Replies: 7
    Last Post: 21st January 2011, 12:08
  4. QString resize() or count()
    By nagabathula in forum Qt Programming
    Replies: 6
    Last Post: 27th December 2010, 12:55
  5. Replies: 4
    Last Post: 31st January 2008, 20:44

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.