Java区分中文英文字符


找了找以前老大写的判断中文字符和英文字符的类

public static int getWordLength(String str) {
   int len = 0;
   if (StringUtil.isBlank(str)) {
    return len;
   }
   char[] ch = str.toCharArray();
   for (int i = 0; i < ch.length; i++) {
    if (isChinese(ch[i])) {
     len = len + 2;
    } else {
     len = len + 1;
    } 
   }
   return len;
}

//GENERAL_PUNCTUATION 判断中文的“号
//CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
//HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号

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;
}

public static void main(String[] args) {
   System.out.println(getWordLength("热”烈"));
}

你可能感兴趣的:(java,C++,c,C#)