Java determines whether a string is Chinese by using Unicode encoding, because the encoding range for Chinese is: 0X4E00--0X9FBB, but the universal range is not very accurate in judging Chinese, because some Chinese punctuation marks use interval judgments to get the wrong result. And the use of interval to judge Chinese efficiency is not high, for example: str.substring (i, i + 1). Matches ("[\ a-\\?] + "), you need to traverse the entire string, if the string is too long and inefficient, and it will be wrong to judge punctuation. Here's an efficient and accurate method of judging, using the character class in Java:
Package test;
Import static java.lang.character.unicodeblock.*;
Import com.google.common.base.Strings; public class Checkcontainchinese {public static void main (string[] args) {String Containchinese
= "Test, I have Chinese";
String Containnochiese = "Test, I don ' t contain Chinese";
System.out.println ("Containchinese contains Chinese:" + Checkstringcontainchinese (Containchinese));
System.out.println ("Containnochiese contains Chinese:" + Checkstringcontainchinese (Containnochiese)); } private static Boolean Checkstringcontainchinese (String checkstr) {if (!
Strings.isnullorempty (CHECKSTR)) {char[] Checkchars = Checkstr.tochararray ();
for (int i = 0; i < checkchars.length i++) {char Checkchar = checkchars[i];
if (Checkcharcontainchinese (Checkchar)) {return true;
}
} return false; } private static Boolean Checkcharcontainchinese (char checkchar) {Character.unicodeblock UB = Chara Cter.
Unicodeblock.of (Checkchar); if (cjk_unified_ideographs = = UB | | Cjk_compatibility_ideographs = = UB | |
Cjk_compatibility_forms = = UB | | Cjk_radicals_supplement = = UB | | Cjk_unified_ideographs_extension_a = = UB | |
Cjk_unified_ideographs_extension_b = = UB) {return true;
return false; }
}
Run Result: