java string 判断中文汉字及中文字符:
package com.boco.synchrodata.util;
public final class StringUtil {
/**
* 根据Unicode编码判断中文汉字和符号
* @param c
* @return
*/
private 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.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
/**
* 完整的判断中文汉字和符号
* @param strName
* @return
*/
public static boolean isChinese(String strName) {
char[] ch = strName.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
return true;
}
}
return false;
}
/**
* 判断包含中文数目
* @param str
* @return
*/
public static long lenWithChinese(String str) {
//去掉中文符号(jdk1.5)
final String _str = str.replaceAll("[\\pP‘’“”]", "");
long count = 0;
char[] ch = _str.toCharArray();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (isChinese(c)) {
count++;
}
}
return count;
}
/**
* @param args
*/
public static void main(String[] args) {
}
}