1、java中判断字符是否为中文
/** * 判断是否为中文字符 * @param c * @return */ public 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; }
2、Java中中文字符转unicode
/** * 把中文转成Unicode码 * @param str * @return */ public String chinaToUnicode(String str){ String result=""; for (int i = 0; i < str.length(); i++){ int chr1 = (char) str.charAt(i); if(chr1>=19968&&chr1<=171941){//汉字范围 u4e00-u9fa5 (中文) result+="\u" + Integer.toHexString(chr1); }else{ result+=str.charAt(i); } } return result; }
知道了Java中中文字符在unicode字符集中的存储规则,也为后面Java中实现字母索引中文建立了基础。