Java determines whether a string has Chinese the general case is the use of Unicode encoding (CJK Unified Chinese Character Coding interval: 0X4E00–0X9FBB) to make a judgment, but in fact, this interval to judge Chinese is not very accurate, because some Chinese punctuation, such as:,. And so on is not recognized.
Import Java.util.regex.Pattern;
public class Charutil {
public static void Main (string[] args) {
string[] Strarr = new string[] {"www.micmiu.com", "!@#$%^&* () _+{}[]|\" '?/:;<>,. ","! ¥ ... ()——:;“”‘'《》,。? , "," Do not Ah "," やめて "," Korean Lady ","??? "};
for (String Str:strarr) {
System.out.println ("===========> test string:" + str);
SYSTEM.OUT.PRINTLN ("Regular judgment Result:" + ischinesebyreg (str) + "--" + ischinesebyname (str));
SYSTEM.OUT.PRINTLN ("Unicode judgment Result:" + Ischinese (str));
System.out.println ("Detailed judgment list:");
char[] ch = str.tochararray ();
for (int i = 0; i < ch.length; i++) {
char C = ch[i];
System.out.println (c + "-->" + (Ischinese (c)?) Yes ": no");
}
}
}
Perfect for judging Chinese characters and symbols based on Unicode encoding
private static Boolean Ischinese (char c) {
Character.unicodeblock UB = Character.UnicodeBlock.of (c);
if (UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS | | ub = = Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A | | UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
|| UB = = Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION | | UB = = Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
|| UB = = Character.UnicodeBlock.GENERAL_PUNCTUATION) {
return true;
}
return false;
}
Complete judgment of Chinese characters and symbols
public static Boolean Ischinese (String strName) {
char[] ch = strname.tochararray ();
for (int i = 0; i < ch.length; i++) {
char C = ch[i];
if (Ischinese (c)) {
return true;
}
}
return false;
}
Only partial CJK characters can be judged (CJK Unified Kanji)
public static Boolean Ischinesebyreg (String str) {
if (str = null) {
return false;
}
Pattern pattern = Pattern.compile ("[\\u4e00-\\u9fbf]+");
Return Pattern.matcher (Str.trim ()). find ();
}
Only partial CJK characters can be judged (CJK Unified Kanji)
public static Boolean ischinesebyname (String str) {
if (str = null) {
return false;
}
Different capitalization: \\p is included, \\p indicates that it does not contain
\\P{CN} is meant to encode characters that are not defined in Unicode, and \\P{CN} represents the encoding of characters that have been defined in Unicode
String reg = "\\P{INCJK Unified IDEOGRAPHS}&&\\P{CN}";
Pattern pattern = Pattern.compile (reg);
Return Pattern.matcher (Str.trim ()). find ();
}
}