`

计算字符串中字节长度

阅读更多

 /*
  * * 计算字符串的字节长度(字母数字计1,汉字及标点计2) *
  */
 public static int byteLength(String string) {
  int count = 0;
  for (int i = 0; i < string.length(); i++) {
   if (Integer.toHexString(string.charAt(i)).length() == 4) {
    count += 2;
   } else {
    count++;
   }
  }
  return count;
 }
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics