How can non-ASCII characters be detected in a QString?

6.1k views Asked by At

I want to detect if the user has inputted a non-ASCII (otherwise incorrectly known as Unicode) character (for example, り) in a file save dialog box. As I am using Qt, any non-ASCII characters are properly saved in a QString, but I can't figure out how to determine if any of the characters in that string are non-ASCII before converting the string to ASCII. That character above ends up getting written to the filesystem as ã‚Š.

4

There are 4 answers

0
László Papp On BEST ANSWER

There is no such a built-in feature in my understanding.

About 1-2 years ago, I was proposing an isAscii() method for QString/QChar to wrap the low-level Unix isacii() and the corresponding Windows function, but it was rejected. You could have written then something like this:

bool isUnicode = !myString.at(3).isAcii();

I still think this would be a handy feature if you can convince the maintainer. :-)

Other than that, you would need to check against the ascii boundary yourself, I am afraid. You can do this yourself as follows:

bool isUnicode = myChar.unicode() > 127; 

See the documentation for details:

ushort QChar::unicode () const

This is an overloaded function.

0
Pali On

To write it in compact way without loop, you can use regular expression:

bool containsNonASCII = myString.contains(QRegularExpression(QStringLiteral("[^\\x{0000}-\\x{007F}]")));
0
Oleg Shparber On

The simplest way is to check every charachter's code (QChar::unicode()) to be below 128 if you need pure 7-bit ASCII.

1
user1495110 On

this works for me : isLetterOrNumber()

ot_id += QChar((short) b.to_ulong()).isLetterOrNumber() ? QChar((short) b.to_ulong()) : QString("");