Java判斷字符串長度
今天一個程序出了問題,排查了半天才發現,原來是因為原來的一個計算漢字長度的代碼出了問題:
public static int getStringRealLength(String str) throws Exception{
String str1 = new String(str.getBytes("GB2312"),"iso-8859-1");
return str1.length();
}
漢字中有生僻字:“暘”,結果計算出長度為1。後來將GB2312改成GBK就可以了。因為字符可以正確算出長度。
原帖地址:https://blog.csdn.net/soundfuture/article/details/11601653
漢字所占byte問題請參考我的博文:https://blog.csdn.net/woshixuye/article/details/7907537
最後更新:2017-04-03 12:54:29