Today, the problem is how to determine the input of the string is Chinese characters, the learning method is reproduced, the record summarized as follows:
Java is a Unicode encoded char variable with a range of 0-65535 unsigned values, which can represent 65,536 characters, and basically the word characters on earth are all included, in practice, we want to judge whether a character is Chinese, or whether characters in a string have Chinese characters to meet business requirements, there is a method in the string class that can get the length of its characters (), see the following example, Java code String s1 = "I am Chinese"; String s2 = "Imchinese"; String s3 = "im Chinese"; System.out.println (s1+ ":" +new String (S1). Length ()); System.out.println (s2+ ":" +new String (S2). Length ()); System.out.println (s3+ ":" +new String (S3). Length ());
OUTPUT:
I am Chinese: 5
Imchinese:9
Im Chinese: 5
See, in a string. If you have double-byte characters in Java, you encode each character in Double-byte code, and if it's a single-byte character, it's single-byte.
So according to the above rules, combined with a QQ nickname. G tea. Zhuhai Brother's tips The following workaround is to determine whether the length of the string and the length of the character byte are the same to determine if there are double-byte characters
Java Code System.out.println (S1.getbytes (). Length = = S1.length ())? S1 No Chinese Characters ":" "S1 has Chinese characters"); System.out.println ((S2.getbytes (). Length = = S2.length ())? " S2 No Chinese Characters ":" "S2 has Chinese characters"); System.out.println ((S3.getbytes (). Length = = S3.length ())? " S3 No Chinese Characters ":" "S3 has Chinese characters");
OUTPUT:
S1 has Chinese characters
S2 without Chinese characters
S3 has Chinese characters//
Wait a minute, it's true that you can tell if a string has double-byte characters, but it's a bit tricky to figure out if there are any characters, and we know there are many other countries where characters are double-byte in Unicode.
Therefore, we need to further determine the encoding range of Chinese characters how to determine it, I used a method that is now Notepad output 0-65535 characters between, through the observation that the first Chinese character is ' one ' last is '. ' (I don't know now); Well, it's easier to judge Chinese characters. For example, we can compare the code range of characters, and finally give you some of the results of my experiment the basic concentration of Chinese characters between [19968,40869], a total of 20,901 Chinese characters (is not a little less, calculate how much you can know)
/**
* To determine if the string is
Chinese string @return
*/Public
static Boolean Ischinese (String string) {
int n = 0;
for (int i = 0; i < string.length (); i++) {
n = (int) string.charat (i);
if (!) ( 19968 <= n && n <40869)) {return
false;
}
}
return true;
}
By the way, record some coding ranges:
Unicode encoding Range:
Kanji: [0x4e00,0x9fa5] (or decimal [19968,40869])
Number: [0x30,0x39] (or decimal [48, 57])
lowercase letters: [0x61,0x7a] (or decimal [97, 122])
Uppercase letters: [0X41,0X5A] (or decimal [65, 90])
See ASCII table:
Http://home.educities.edu.tw/wanker742126/asm/ap04.html
Unicode Chinese Range
http://www.iteye.com/topic/558050
Encoding Range: \u4e00-\u9fa5
Double-byte character encoding range: \u0391-\uffe5
two-byte character encoding range
1. GBK (gb2312/gb18030)
/x00-/xff GBK Two-byte coding range
/x20-/x7f ASCII
/xa1-/xff Chinese gb2312
/x80-/xff Chinese GBK
2. UTF-8 (Unicode)
/u4e00-/u9fa5 (Chinese)
/x3130-/x318f (Korean
/XAC00-/XD7A3 (Korean)
/u0800-/u4e00 (Japanese)