Whether the Java judgment string contains Chinese __java

Source: Internet
Author: User

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:

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.