Java code
Import java. util. regex. Matcher;
Import java. util. regex. Pattern;
Public class IfHanZi {
Public static void main (String [] args ){
// Method 1:
String s1 = "I am a Chinese ";
String s2 = "imchinese ";
String s3 = "im Chinese ";
System. out. println (s1 + ":" + new String (s1). length ());
System. out. println (s2 + ":" + new String (s2). length ());
System. out. println (s3 + ":" + new String (s3). length ());
System. out. println (s1.getBytes (). length = s1.length ())? "S1 no Chinese characters"
: "S1 has Chinese characters ");
System. out. println (s2.getBytes (). length = s2.length ())? "S2 has no Chinese characters"
: "S2 has Chinese characters ");
System. out. println (s3.getBytes (). length = s3.length ())? "S3 has no Chinese characters"
: "S3 has Chinese characters ");
// Method 2:
Int count = 0;
String regEx = "[\ u4e00-\ u9fa5]";
String str = "Chinese fd I am a Chinese ";
Pattern p = Pattern. compile (regEx );
Matcher m = p. matcher (str );
While (m. find ()){
For (int I = 0; I <= m. groupCount (); I ++ ){
Count = count + 1;
}
}
System. out. println ("Total" + count + "count ");
}
}
Author's "J2EE technology discussion"