资源介绍
针对Oracle数据库中汉字在不通的字符集下所占字符长度不通,在保存时可能需要对源字符串的长度进行判断。
实现过程如下
/**
* 针对汉字 进行计算长度
* @param string 计算的字符串
* @param shuJuKuHanZhiChangDu 汉字所占字符大小
* @return int 字符串长度
*/
public static int stringOfSize(String string,int shuJuKuHanZhiChangDu) {
int sum = 0;
shuJuKuHanZhiChangDu--;
if(string==null || string.length()==0){
return sum;
}
for (char charAt : string.toCharArray()){
sum++;
if(isChinese(charAt)){
sum = sum+shuJuKuHanZhiChangDu;
}
}
return sum;
}
/**
* 根据Unicode编码完美的判断中文汉字和符号
* @param c 要判断的 字符
* @return boolean
*/
private static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
return 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;
}