Results 1 to 3 of 3

Thread: is QString empty?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. 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, 21:06
  2. QTreeView is empty
    By t0bias in forum Qt Programming
    Replies: 2
    Last Post: 31st August 2008, 11:22
  3. Replies: 4
    Last Post: 31st January 2008, 20:44
  4. how to copy part of QString to anothe QString
    By nass in forum Qt Programming
    Replies: 1
    Last Post: 26th March 2007, 19:05
  5. remove directory empty or not empty
    By raphaelf in forum Newbie
    Replies: 12
    Last Post: 27th October 2006, 07: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
  •  
Qt is a trademark of The Qt Company.