Results 1 to 3 of 3

Thread: is QString empty?

  1. #1
    Join Date
    Aug 2009
    Posts
    122
    Thanks
    74
    Qt products
    Qt4
    Platforms
    Windows

    Default is QString empty?

    What is the best way to check if a QString contains only empty spaces.

    isEmpty() or isNull() don't do what I want.

    I need to know if my QString contains a) only empty spaces (on any length), or b) any combination of symbols and empty spaces. What is the fastest way to do that check?

    Thanks!

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: is QString empty?

    a) if QString.count(' ') == QString.length()
    b) QString.count(regex) == QString.length()

    Probably not the fastest (I'd imagine using constData and counting them using a char * pointer for that), but the most easily implemented (apart from 'b' if you don't know regex's)

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

    timmu (17th January 2010)

  4. #3
    Join Date
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: is QString empty?

    Quote Originally Posted by timmu View Post
    I need to know if my QString contains a) only empty spaces (on any length), or b) any combination of symbols and empty spaces. What is the fastest way to do that check?
    If “fastest” means “fastest to code” and “empty spaces” means “any whitespace, like spaces, tabs, new lines, etc.” then use QString::trimmed:
    Qt Code:
    1. if (qstr.trimmed().isEmpty()) /* nothing but whitespace */;
    To copy to clipboard, switch view to plain text mode 

    If “fastest” means “fastest at runtime”... I’m not sure, but I’d guess applying wcsspn to the return value of QString::constData would be a good try:
    Qt Code:
    1. const wchar_t* wideCharacterString = (const wchar_t*) qstr.constData();
    2. unsigned int offsetOfFirstNonBlank = wcsspn(wideCharacterString, L" ");
    3. if (!*(wideCharacterString+offsetOfFirstNonBlank)) {
    4. /* offset locates the zero terminator: the string is all blanks */
    5. }
    To copy to clipboard, switch view to plain text mode 

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

    timmu (17th January 2010)

Similar Threads

  1. How to put empty value in QDateEdit?
    By sawerset in forum Qt Programming
    Replies: 5
    Last Post: 14th October 2019, 22:06
  2. QTreeView is empty
    By t0bias in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2008, 12:22
  3. Replies: 4
    Last Post: 31st January 2008, 21:44
  4. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 20:05
  5. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 08:30

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.