如何检查字符串是否仅包含ASCII? [英] How to check if a String contains only ASCII?
问题描述
如果字符是字母,则呼叫Character.isLetter(c)
返回true
.但是,是否有一种方法可以快速查找String
是否仅包含ASCII的基本字符?
The call Character.isLetter(c)
returns true
if the character is a letter. But is there a way to quickly find if a String
only contains the base characters of ASCII?
推荐答案
来自番石榴 19.0起,您可以使用:
From Guava 19.0 onward, you may use:
boolean isAscii = CharMatcher.ascii().matchesAllOf(someString);
这使用 ascii()
而不是现在已弃用的ASCII
单例.
此处的ASCII包括所有ASCII字符,包括低于0x20
(空格)的不可打印字符,例如制表符,换行/返回以及BEL
,其代码为0x07
和DEL
,代码为0x7F
.
Here ASCII includes all ASCII characters including the non-printable characters lower than 0x20
(space) such as tabs, line-feed / return but also BEL
with code 0x07
and DEL
with code 0x7F
.
即使在早期版本的注释中指出了代码点,该代码也会错误地使用字符而不是代码点.幸运的是,创建值U+010000
或更大的代码点所需的字符使用两个替代字符,其值在ASCII范围之外.因此,即使对于包含表情符号的字符串,该方法仍然可以成功测试ASCII.
This code incorrectly uses characters rather than code points, even if code points are indicated in the comments of earlier versions. Fortunately, the characters required to create code point with a value of U+010000
or over uses two surrogate characters with a value outside of the ASCII range. So the method still succeeds in testing for ASCII, even for strings containing emoji's.
对于没有ascii()
方法的早期Guava版本,您可以编写:
For earlier Guava versions without the ascii()
method you may write:
boolean isAscii = CharMatcher.ASCII.matchesAllOf(someString);
这篇关于如何检查字符串是否仅包含ASCII?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!