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