简体   繁体   中英

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

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 ã‚Š .

最简单的方法是检查每个charachter的代码( QChar :: unicode() )是否低于128,如果你需要纯7位ASCII

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.

要在没有循环的情况下以紧凑方式编写它,您可以使用正则表达式:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM