PDA

View Full Version : is QString empty?



timmu
16th January 2010, 22:08
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!

squidge
16th January 2010, 22:59
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)

Coises
16th January 2010, 23:17
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:
if (qstr.trimmed().isEmpty()) /* nothing but whitespace */;

If “fastest” means “fastest at runtime”... I’m not sure, but I’d guess applying wcsspn (http://www.opengroup.org/onlinepubs/9699919799/functions/wcsspn.html) to the return value of QString::constData would be a good try:
const wchar_t* wideCharacterString = (const wchar_t*) qstr.constData();
unsigned int offsetOfFirstNonBlank = wcsspn(wideCharacterString, L" ");
if (!*(wideCharacterString+offsetOfFirstNonBlank)) {
/* offset locates the zero terminator: the string is all blanks */
}