java获取字符串的字节长度

 1 public static int getLength(String s) {
 2     int length = 0;
 3     for (int i = 0; i < s.length(); i++) {
 4         int ascii = Character.codePointAt(s, i);
 5         if (ascii >= 0 && ascii <= 255) {
 6             length++;
 7         } else {
 8             length += 2;
 9         }
10     }
11     return length;
12 }

 

posted @ 2018-06-28 09:08  派小星  阅读(12720)  评论(0编辑  收藏  举报