软件编程
位置:首页>> 软件编程>> java编程>> java中计算字符串长度的方法及u4E00与u9FBB的认识

java中计算字符串长度的方法及u4E00与u9FBB的认识

  发布时间:2022-07-15 18:28:20 

标签:字符串长度,u4E00,u9FBB

字符串采用unicode编码的方式时,计算字符串长度的方法找出UNICODE编码中的汉字的代表的范围“\u4E00” 到“\u9FBB”之间,由于一个汉字代表两个字符所以可以采用如下方法进行计算字符串的长度


public static int getCharLength(String content) {
int count = 0;
for (int i = 0, len = content.length(); i < len; ++i) {
if ('\u4E00' <= content.charAt(i) && '\u9FBB' >= content.charAt(i)) {
count++;
}
}
return content.length()+count;
}


其它的编码方式可以按照这个原理去时行计算

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com