
Originally Posted by
timmu
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 (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:
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 */
}
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 */
}
To copy to clipboard, switch view to plain text mode
Bookmarks