閱讀509 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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

  上一篇:go Oracle中“無監聽程序”和“協議適配器錯誤”的解決方法
  下一篇:go java.lang.NoClassDefFoundError:org/apache/commons/lang/exception/NestableRuntimeException 異常的處理