有时候我们需要知道字符串有多少个字节,比如Oracle中保存内容是按字节的长度的,因为中文和因为是不同的,所以需要写一个工具类来获取。
/**
* 根据指定的字节长度截取字符串
* @param orignal : 原始字符串
* @param count : 要截取的字节长度
* @return
*/
public static String substringByByteLength(String orignal, int count){
if(orignal != null && !"".equals(orignal)){
int tmp = count;
try {
orignal = new String(orignal.getBytes("GBK"),"GBK");
System.out.println(orignal.getBytes("GBK").length);
if(tmp > 0 && tmp < orignal.getBytes("GBK").length){
System.out.println("截取前的值:"+orignal);
StringBuffer buff = new StringBuffer();
char c;
for(int i = 0; i < tmp; i++){
c = orignal.charAt(i);
if((buff.toString() + String.valueOf(c)).getBytes("GBK").length <= count ){
buff.append(c);
}
if(isChinese(c)){
--tmp; //遇到中文汉字,截取字节总数减1
}
}
String str = buff.toString();
System.out.println("截取后的值:"+str);
return str;
}
return orignal;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return "";
}
public static boolean isChinese(char ch) {
//获取此字符的UniCodeBlock
Character.UnicodeBlock ub = Character.UnicodeBlock.of(ch);
// GENERAL_PUNCTUATION 判断中文的“号
// CJK_SYMBOLS_AND_PUNCTUATION 判断中文的。号
// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的,号
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;
}