/**
* 根据字符集类型统计字符串中字节数
* @param str
* @param charset
* @return 字节数
* @throws UnsupportedEncodingException
*/
public static int countByteByCharset(String str, String charset) throws UnsupportedEncodingException{
int count = 0;
if(StringUtils.isEmpty(str)){
return 0;
}
if("ISO8859-1".equalsIgnoreCase(charset.toUpperCase())){
count = str.getBytes("ISO8859-1").length;
} else if("GB2312".equalsIgnoreCase(charset.toUpperCase())){
count = str.getBytes("GB2312").length;
} else if("GBK".equalsIgnoreCase(charset.toUpperCase())){
count = str.getBytes("GBK").length;
} else if("UTF-8".equalsIgnoreCase(charset)){
count = str.getBytes("UTF-8").length;
}
return count;
}
测试类:
public static void main(String[] args) throws UnsupportedEncodingException {
String str = "7只需3000元-7plus只需3500元 6s只需2000-6splus只需2200 6 只需1600-6p只需1800 5S:900-5Se:1200 国行正品 支持全国联保 支持貨捯附款 支持紛期附款 加昵称上的号咨询购 维 姓 号 K F C 3 6 2";
System.out.println("ISO8859-1:" + countByteByCharset(str, "ISO8859-1"));
System.out.println("GB2312:" + countByteByCharset(str, "GB2312"));
System.out.println("GBK:" + countByteByCharset(str, "GBK"));
System.out.println("UTF-8:" + countByteByCharset(str, "UTF-8"));
}
运行结果:
ISO8859-1:131
GB2312:178
GBK:181
UTF-8:231