Java judgment character for Chinese instance Code (super Works) _java

Source: Internet
Author: User

In the project will often encounter a project needs to determine the characters are some of the problems of Chinese, so the collection of the text of the judgement of the Code fragment, hereby share for your reference.

The code is posted directly, with detailed comments in it.

Package com.coder4j.main;
Import Java.util.regex.Pattern; /** * Java judgment Chinese Character * * @author Chinaxiang * @date 2015-08-11 */public class Checkchinese {public static void main (Stri Ng[] args) {//pure english String s1 = "hello,tom.! @#$%^&* () _+-={}|
[];':\"?";
Pure Chinese (excluding Chinese punctuation) String s2 = "Hello China"; Pure Chinese (including Chinese punctuation) String s3 = "Hello, China." 《》:“”‘';()【】!
¥, ";
Korean String s4 = "한국어난";
Japanese String s5 = "ぎじゅつ";
Special character String s6 = "";
String s7 = "╃";
String s8 = "╂";
Traditional Chinese String S9 = "flies teacher"; 1 Use character range to determine whether System.out.println ("S1 contains Chinese:" + haschinesebyrange (S1));/False System.out.println ("S2 contains Chinese:" + Haschinesebyrange (S2));/True System.out.println ("S3 contains Chinese:" + haschinesebyrange (S3));/True System.out.println ("
S4 contains Chinese: "+ haschinesebyrange (S4));/False System.out.println (" S5 contains Chinese: "+ haschinesebyrange (s5));/False System.out.println ("S6 contains Chinese:" + haschinesebyrange (S6));/False System.out.println ("S7 contains Chinese:" + haschinesebyrange ( S7));//False System.out.println ("S8 contains Chinese:" + haschinesebyrange (s)8)//False System.out.println ("S9 contains Chinese:" + haschinesebyrange (S9));/True System.out.println ("-------Split line-------")
; System.out.println ("S1 is all Chinese:" + ischinesebyrange (S1));/False System.out.println ("S2 is all Chinese:" + ischinesebyrange (s2 )/True System.out.println ("S3 is all Chinese:" + ischinesebyrange (S3));/False Chinese punctuation is not in range System.out.println ("S4 is all Chinese:" + Ischinesebyrange (S4));/False System.out.println ("S5 is all Chinese:" + ischinesebyrange (s5));/False System.out.println ("
S6 is all Chinese: "+ ischinesebyrange (S6));/False System.out.println (" S7 is all Chinese: "+ ischinesebyrange (s7));/False System.out.println ("S8 is all Chinese:" + ischinesebyrange (S8));/False System.out.println ("S9 is all Chinese:" + ischinesebyrange (S9
)/True System.out.println ("-------Split line-------"); 2 Use the character range regular judgment (Result same 1) System.out.println ("S1 contains Chinese:" + haschinesebyreg (S1));/False System.out.println ("S2 contains Chinese:" + Haschinesebyreg (S2));/True System.out.println ("S3 contains Chinese:" + haschinesebyreg (S3));/True System.out.println (" S4 contains Chinese: "+ haschinesebyreg(S4)); /False System.out.println ("S5 contains Chinese:" + haschinesebyreg (s5));/False System.out.println ("S6 contains Chinese:" + Haschinesebyreg (S6));/False System.out.println ("S7 contains Chinese:" + haschinesebyreg (s7));/False System.out.println ("
S8 contains Chinese: "+ haschinesebyreg (S8));/False System.out.println (" S9 contains Chinese: "+ haschinesebyreg (S9));/True
System.out.println ("-------Split line-------"); System.out.println ("S1 is all Chinese:" + ischinesebyreg (S1));/False System.out.println ("S2 is all Chinese:" + ischinesebyreg (S2)); /True System.out.println ("S3 is all Chinese:" + ischinesebyreg (S3));/False Chinese punctuation is not in range System.out.println ("S4 is all Chinese:" + Ischinesebyreg (S4));/False System.out.println ("S5 is all Chinese:" + ischinesebyreg (s5));/False System.out.println ("
S6 is all Chinese: "+ ischinesebyreg (S6));/False System.out.println (" S7 is all Chinese: "+ ischinesebyreg (s7));/False System.out.println ("S8 is all Chinese:" + ischinesebyreg (S8));/False System.out.println ("S9 is all Chinese:" + ischinesebyreg (S9));
/True System.out.println ("-------Split line-------"); 3 Use the CJK character set to determine System.Out.println ("S1 contains Chinese:" + Haschinese (S1));/False System.out.println ("S2 contains Chinese:" + haschinese (s2));/True
System.out.println ("S3 contains Chinese:" + Haschinese (S3));/True System.out.println ("S4 contains Chinese:" + Haschinese (S4));/False
System.out.println ("S5 contains Chinese:" + Haschinese (s5));/False System.out.println ("S6 contains Chinese:" + Haschinese (S6));/False
System.out.println ("S7 contains Chinese:" + Haschinese (s7));/False System.out.println ("S8 contains Chinese:" + Haschinese (S8));/False
System.out.println ("S9 contains Chinese:" + Haschinese (S9));/True System.out.println ("-------Split line-------");
System.out.println ("S1 is all Chinese:" + Ischinese (S1));/False System.out.println ("S2 is all Chinese:" + ischinese (s2));/True System.out.println ("S3 is all Chinese:" + Ischinese (S3));/True Chinese punctuation is also included in System.out.println ("S4 is all Chinese:" + Ischinese (S4)); False System.out.println ("S5 is all Chinese:" + Ischinese (s5));/False System.out.println ("S6 is all Chinese:" + Ischinese (S6)); False System.out.println ("S7 is all Chinese:" + Ischinese (s7));/False System.out.println ("S8 is all Chinese:" + IschinESE (S8));//False System.out.println ("S9 is all Chinese:" + Ischinese (S9));/true}/** * contains Chinese characters <br> * contains punctuation <br> * @param str * @return/public static Boolean Haschinese (String str) {if (str = null) {return false;} char[] Ch
= Str.tochararray ();
for (char c:ch) {if (Ischinese (c)) {return true;}} return false; 
/** * is full of Chinese characters <br> * contains punctuation <br> * @param str * @return/public static Boolean Ischinese (String str) {
if (str = null) {return false;} char[] ch = str.tochararray ();
for (char c:ch) {if (!ischinese (c)) {return false;}} return true; /** * is a Chinese character <br> * contains punctuation <br> * @param c * @return/private static Boolean Ischinese (char c) {Chara Cter.
Unicodeblock UB = Character.UnicodeBlock.of (c); if (UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS) {return true;} else if (UB = = Character.UnicodeBlock.CJK_COMPAT Ibility_ideographs) {return true;} else if (UB = = Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION) {REturn true; else if (UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A) {return true;} else if (UB = = Character.unic Odeblock.cjk_unified_ideographs_extension_b) {return true;} else if (UB = = Character.UnicodeBlock.CJK_UNIFIED_ Ideographs_extension_c) {return true;} else if (UB = = Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_D) {Retu
RN true; else if (UB = = Character.UnicodeBlock.GENERAL_PUNCTUATION) {return true;} else if (UB = = Character.UnicodeBlock.HALFWI
Dth_and_fullwidth_forms) {return true;} return false; /** * contains Chinese characters <br> * Judged according to encoding range <br> * CJK Unified Kanji (not included in Chinese). 《》()“‘'”、!  ¥) <br> * * @param str * @return/public static Boolean Haschinesebyreg (String str) {if (str = null) {return
False
Pattern pattern = Pattern.compile ("[\\u4e00-\\u9fbf]+");
return Pattern.matcher (str). find (); /** * are all Chinese characters <br> * According to the encoding range of the judgment <br> * CJK Unified Chinese characters (does not contain Chinese,. 《》()“‘'”、! ¥ <br> * * @param str * @return/public static Boolean Ischinesebyreg (String str) {if (str = null) {return false;}
Pattern pattern = Pattern.compile ("[\\u4e00-\\u9fbf]+");
return Pattern.matcher (str). matches (); /** * contains Chinese characters <br> * Judged according to encoding range <br> * CJK Unified Kanji (not included in Chinese). 《》()“‘'”、! ¥) <br> * * @param str * @return/public static Boolean Haschinesebyrange (String str) {if (str = null) {Retu
RN false;
} char[] ch = str.tochararray ();
for (char c:ch) {if (c >= 0x4e00 && C <= 0x9fbf) {return true;}} return false; /** * are all Chinese characters <br> * According to the encoding range of the judgment <br> * CJK Unified Chinese characters (does not contain Chinese,. 《》()“‘'”、! ¥) <br> * * @param str * @return/public static Boolean Ischinesebyrange (String str) {if (str = null) {retur
n false;
} char[] ch = str.tochararray ();
for (char c:ch) {if (c < 0x4e00 | | | | c > 0X9FBF) {return false;}} return true; }
}

If only to determine whether it is Chinese, do not need to judge Chinese punctuation, it is recommended to use the regular to match, may be more efficient point.

The above code content to introduce the Java Judgment characters for Chinese instance Code (super Works), I hope to help you.

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.