how check if String has Full width character in java

烈酒焚心 提交于 2019-12-01 03:23:24

问题


Can anyone suggest me how to check if a String contains full width characters in Java? Characters having full width are special characters.

Full width characters in String:

abc@gmail.com

Half width characters in String:

abc@gmail.com

回答1:


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]

So the method then looks like:

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._]*");
}



回答2:


You can compare the UNICODE Since unicode for alphabets (a-z) is 97-122 , So you can easily diffrentiate between the two

String str="abc@gmail.com";
System.out.println((int)str.charAt(0));

for Input

abc@gmail.com

Output

65345



回答3:


You can try something like this:

public static final String FULL_WIDTH_CHARS = "AaBbCcDdEeFfGgHhIiJj"
                      + "KkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";

public static boolean containsFullWidthChars(String str) {
    for(int i = 0; i < FULL_WIDTH_CHARS.length(); i++) {
        if(str.contains(String.valueOf(FULL_WIDTH_CHARS.charAt(i)))) {
            return true;
        }
    }
    return false;
}



回答4:


use regular expression here. \W is used to check for non-word characters.

str will contain full width character if following statement return true:

boolean flag = str.matches("\\W");



来源:https://stackoverflow.com/questions/29508932/how-check-if-string-has-full-width-character-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!