`
reginaminlee
  • 浏览: 32567 次
  • 性别: Icon_minigender_2
  • 来自: 湖南
社区版块
存档分类
最新评论

获取字符串中的中文字符长度

    博客分类:
  • java
阅读更多
Java代码 复制代码 收藏代码
  1. /**  
  2.      * 获取字符串中的中文字符长度  
  3.      * @param str  
  4.      * @return  
  5.      */  
  6.     public static int getChineseCharacters(String str){   
  7.         byte chars[]=str.getBytes();   
  8.         String cc="";   
  9.         byte temp[]=new byte[2];   
  10.         for(int i=0, count=0; i<chars.length; i++){   
  11.             if(chars[i]<0){   
  12.                 temp[count]=chars[i];   
  13.                 count++;   
  14.                 if(count%2==0){   
  15.                     cc+=new String(temp);   
  16.                     count=0;   
  17.                 }   
  18.             }   
  19.         }   
  20.         return cc.length();   
  21.     }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics