Home > Java > JavaBase > body text

Java determines whether a string is Chinese

Release: 2019-12-28 13:31:54
Original
5868 people have browsed it

Java determines whether a string is Chinese

The method code for java to determine whether a string is Chinese is as follows:

String string; //需要判断的内容
if(string.matches("[\u4E00-\u9FA5]+")){
    ToastUtils.Toast(Activity_authentication.this, "内容是中文");
}else{
    ToastUtils.Toast(Activity_authentication.this, "内容包含非中文");
}
Copy after login

Unicode is a unified global text encoding. It assigns a unique code to each character of various texts in the world, enabling cross-language and cross-platform applications.

Chinese users are most often exposed to the Unicode encoding of Chinese characters. The number of Chinese characters is huge. There are thousands of Chinese characters in daily use. When adding rare characters, the number reaches tens of thousands.

matches() method is used to detect whether a string matches a given regular expression.

The str.matches(regex) form of calling this method produces exactly the same result as the following expression:

Pattern.matches(regex, str)
Copy after login

Syntax

public boolean matches(String regex)
Copy after login

Parameters: regex -- Match string regular expression.

Return value: Returns true when the string matches the given regular expression.

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

The above is the detailed content of Java determines whether a string is Chinese. 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