`

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

    博客分类:
  • java
阅读更多
/**
	 * 获取字符串中的中文字符长度
	 * @param str
	 * @return
	 */
	public static int getChineseCharacters(String str){
		byte chars[]=str.getBytes();
		String cc="";
		byte temp[]=new byte[2];
		for(int i=0, count=0; i<chars.length; i++){
			if(chars[i]<0){
				temp[count]=chars[i];
				count++;
				if(count%2==0){
					cc+=new String(temp);
					count=0;
				}
			}
		}
		return cc.length();
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics