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!