我想知道是否有办法检测用户的输入是否在希腊字符集中.
编辑:
为了更清楚,我需要识别用户输入的语言而不是手机的语言环境.
例如,我的手机是英文的,假设我的键盘是俄语,getDefault()返回"en",但我需要"ru".
我不知道这是否可以从Android开箱即用,也许是一种检测字符串字符代码的方法,看看是英文字母还是其他字母.这有什么意义吗?
我想象的东西if character belongs to K then is English (where K is the essemble of english characters)
解:
最后我使用正则表达式来确定字符串是否是英文的.
String pattern = "^[A-Za-z0-9. ]+$";
if (string.matches(pattern)
// is English
else
// is not English
Run Code Online (Sandbox Code Playgroud)
如果有人必须提出更好的解决方案,我会将其标记为答案.
您可以使用以下方法而不是模式匹配:
boolean isEnglish = true;
for ( char c : s.toCharArray() ) {
if ( Character.UnicodeBlock.of(c) != Character.UnicodeBlock.BASIC_LATIN ) {
isEnglish = false;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
Locale.getDefault().getLanguage().equals("gr")
Run Code Online (Sandbox Code Playgroud)
另一种方式:
contains(Charset)
Run Code Online (Sandbox Code Playgroud)
编辑:
经过一段时间的浏览,我遇到了CharsetDetector和Character SetDetector。
这里你有方法detect(),但不确定如何最好地利用它。
| 归档时间: |
|
| 查看次数: |
4494 次 |
| 最近记录: |