Home > Java > JavaBase > body text

How to determine whether a string is Chinese in java

Release: 2019-11-22 09:28:17
Original
3874 people have browsed it

How to determine whether a string is Chinese in java

Java uses Unicode encoding. The range of char type variables is 0-65535. Unsigned values ​​can represent 65536 characters. Basically, all characters on the earth can be included.

Chinese characters are basically concentrated between [19968, 40869], with a total of 20901 Chinese characters.

unicode encoding range:

Chinese characters: [0x4e00,0x9fa5] (or decimal [19968,40869])

Numbers: [0x30,0x39] (or decimal [48 , 57])

Lowercase letters: [0x61,0x7a] (or decimal [97, 122])

Uppercase letters: [0x41,0x5a] (or decimal [65, 90])

The first method is to judge whether there are Chinese characters

public boolean checkcountname(String countname)
    {
         Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
            Matcher m = p.matcher(countname);
            if (m.find()) {
                return true;
            }
            return false;
    }
Copy after login

Use regular expressions to match

The second method is to judge whether the entire string is composed of Chinese characters

public boolean checkname(String name)
    {
        int n = 0;
        for(int i = 0; i < name.length(); i++) {
            n = (int)name.charAt(i);
            if(!(19968 <= n && n <40869)) {
                return false;
            }
        }
        return true;
    }
Copy after login

For more java knowledge, please pay attention to java basic tutorial.

The above is the detailed content of How to determine whether a string is Chinese in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!