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 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM