汉字的UNICODE范围在19968~171941之间。//CountChinese.java public class CountChinese { public static boolean isChinese(char a) { int v=(int)a; return (v>=19968 && v<=171941); }
public static void main(String[] args) { String str=new String("abc中d华112人e民**^$%*&()共)和ggafda,.国[]l;"); int cnt=0;
public class CountChinese
{
public static boolean isChinese(char a)
{
int v=(int)a;
return (v>=19968 && v<=171941);
}
public static void main(String[] args)
{
String str=new String("abc中d华112人e民**^$%*&()共)和ggafda,.国[]l;");
int cnt=0;
for(int i=0;i<str.length();i++)
{
if(isChinese(str.charAt(i)))
cnt++;
}
System.out.println("共有汉字:"+cnt+" 个。");
}
}
汉字的UNICODE范围在19968~171941之间。
-------------------------------------------------------------------------这数字从哪里来的?
我看UnicodeBlock的源代码,发现汉字所在的Block范围是 0x4E00 <= ch < 0xA000
就是 19968~40960。 你的上限171941似乎太大了吧,很多外字都被你包括进来了。
的代码我试过了没有问题