How can I check if a QString contains only "invisible" characters?
I would like to check if a QString
is made up of only non-printable or non-visible characters. The QString
could contain unicode...
I imagine a regular expression may work, but I have no idea how to create such a regex.
How can I check if a QString
contains only "invisible" characters ? (space, n
, r
, t
...)
My "brute force" try
bool checkIfEmpty(const QString &contents) const
{
for(QString::const_iterator itr(contents.begin()); itr != contents.end(); ++itr)
{
if(*itr != 'n' && *itr != 'r' && *itr != ' ' && *itr != 't')
return false;
}
return true;
}
A QString
is made up of UTF-16 code units, puzzlingly named QChar
, not characters. A character may be represented by one or more Unicode code points. Generally speaking, you'd need to iterate over the string and process all surrogate pairs to get Unicode code points (in UTF-32/UCS-4) through QChar::surrogateToUcs4
. You then get QChar::category
for these, and check what they are. This could be represented in a regex that still operates on QChar
s, though.
Thankfully, all of the non-printing code points are represented as a single code unit in UTF-16, so by looking at each QChar
in isolation you can tell what it is.
And, QChar::isSpace()
knows all that, and QRegExp
matches such characters under the s
category.
Thus, your check reduces to:
bool isWhiteSpace(const QString & str)
{
return QRegExp("s*").exactMatch(str);
}
It'd be useful to retain the regular expression as a class member, since its construction from a pattern is expensive:
// C++11, Qt 5
class C {
QRegExp m_whiteSpace { QStringLiteral("s*") };
public:
bool isWhiteSpace(const QString & str) const {
return m_whiteSpace.exactMatch(str);
}
};
// C++98, Qt 5
class C {
QRegExp m_whiteSpace;
public:
C() : m_whiteSpace(QStringLiteral("s*")) {}
bool isWhiteSpace(const QString & str) const {
return m_whiteSpace.exactMatch(str);
}
};
// C++98, Qt 4
class C {
QRegExp m_whiteSpace;
public:
C() : m_whiteSpace(QLatin1String("s*")) {}
bool isWhiteSpace(const QString & str) const {
return m_whiteSpace.exactMatch(str);
}
};
The QString could contain unicode
Not could. Does. Always. That's what a QString
is. It is a container for UTF-16 code units. They "are" Unicode, in the sense of having an interpretation defined in the Unicode standard. The Latin-1 block is no less Unicode than the Devanagari block.
What you meant was probably that the string's contents aren't limited to any subset of Unicode code points or blocks.
try this approach
bool checkIfEmpty(const QString contents) const
{
if(contents.trimmed()=="") return true;
else return false;
}
please note that this could be used only if you meant by "no printable" is space or tab characters
链接地址: http://www.djcxy.com/p/60836.html上一篇: Mysql:隐形字符