如何检查String中是否包含全角字符 [英] how check if String has Full width character in java
本文介绍了如何检查String中是否包含全角字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人可以建议我如何检查 String
是否包含 Java
中的全角字符?全角的字符是特殊字符。
Can anyone suggest me how to check if a String
contains full width characters in Java
? Characters having full width are special characters.
字符串中的全角字符:
abc@gmail.com
字符串中的半角字符:
abc@gmail.com
推荐答案
我不确定您是否正在寻找任何或所有,所以这里有两个函数:
I'm not sure if you are looking for any or all, so here are functions for both:
public static boolean isAllFullWidth(String str) {
for (char c : str.toCharArray())
if ((c & 0xff00) != 0xff00)
return false;
return true;
}
public static boolean areAnyFullWidth(String str) {
for (char c : str.toCharArray())
if ((c & 0xff00) == 0xff00)
return true;
return false;
}
至于你的半宽'。'
和可能的'_'
。首先用替换它们将它们剥离出来:
As for your half width '.'
and possible '_'
. Strip them out first with a replace maybe:
String str="abc@gmail.com";
if (isAllFullWidth(str.replaceAll("[._]","")))
//then apart from . and _, they are all full width
正则表达式
或者,如果您想使用正则表达式进行测试,那么这是全宽的实际字符范围:
Regex
Alternatively if you want to use a regex to test, then this is the actual character range for full width:
[\uFF01-\uFF5E]
所以方法如下:
public static boolean isAllFullWidth(String str) {
return str.matches("[\\uff01-\\uff5E]*");
}
您可以添加其他字符,因此不需要剥离它们:
You can add your other characters to it and so not need to strip them:
public static boolean isValidFullWidthEmail(String str) {
return str.matches("[\\uff01-\\uff5E._]*");
}
这篇关于如何检查String中是否包含全角字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文