search
HomeJavaJavaBaseHow to determine whether a string is Chinese in java

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;
    }

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;
    }

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!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor