Java Perfect method to judge Chinese characters _java

Source: Internet
Author: User

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.

The following is a relatively perfect method of judgment: Charutil.java

Copy Code code as follows:

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}&AMP;&AMP;\\P{CN}";
Pattern pattern = Pattern.compile (reg);
Return Pattern.matcher (Str.trim ()). find ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.