UnicodeBlock检测中文用法

/**
  * 判断输入的是汉字
  */
 public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
             || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
            || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
            || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
            || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
            || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
 /**
  * 检测输入String是否全是中文
  */
 public static boolean checkNameChese(String name){
  boolean res=true;
  char[] ch=name.toCharArray();
  for(int i=0;i<name.length();i++){ //检测每一个字符是否是中文
   if(!isChinese(ch[i])){
    res=false;
    break; //有一个不是中文立即跳出
   }
  }return res;
 }

你可能感兴趣的:(UnicodeBlock)