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

阅读更多
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
  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.     }  

你可能感兴趣的:(java)