Java 获取字符长度

 1 /**
 2      * 
 3      * @param 字符串
 4      * @return 字符长度
 5      */
 6     public int getWordCount(String str) {
 7         int length = 0;
 8         for (int i = 0; i < str.length(); i++) {
 9             int ascii = Character.codePointAt(str, i);
10             if (ascii >= 0 && ascii <= 255) {
11                 length++;
12             } else {
13                 length += 2;
14             }
15         }
16         return length;
17     }

 

posted @ 2014-11-27 09:49  逍遥散人95  阅读(532)  评论(0编辑  收藏  举报