计算含有中文字符字符串长度的函数

来源:互联网 发布:在淘宝买苹果6好不好 编辑:程序博客网 时间:2024/05/19 03:44
 

public class Test {

 /**
  * @param args
  */
 public static int getWholeLength(String str) {
  int result = 0;
  byte[] b = str.getBytes();
  for (int i = 0; i < b.length; i++) {
   if (b[i] >= 0) {
    System.out.println(b[i]);
    result = result + 1;
   } else {
    System.out.println(b[i]);
    result = result + 3;// 一个汉字等于3个字符
    i++;
   }
  }
  System.out.println(result);
  System.out.println("#####################");
  return result;
 }
 public static void main(String[] args) {
  
  getWholeLength("09");
  getWholeLength("az");
  getWholeLength("AZ");
  getWholeLength("a0我");
 }

}

原创粉丝点击