Method for Determining whether Java string contains Chinese Characters

Source: Internet
Author: User
The method for determining whether Java string contains Chinese characters-general Linux technology-Linux programming and kernel information. The following is a detailed description. Java uses Unicode-encoded char variables in the range of 0-65535 unsigned values, which can represent 65536 characters. Basically, all characters on the earth can be included, in reality, we want to determine whether a character is a Chinese character or whether a character in a string contains a Chinese character to meet business needs, the String class has such a method to get its character length (). See the following example,
String s1 = "I am a 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 a Chinese: 5
Imchinese: 9
Im Chinese: 5
As you can see, if the string contains double-byte characters, java will encode each character in double-byte format. If it is a single-byte character, it will be encoded in single-byte format.
So according to the above rules, combined with a QQ nickname? G tea? I Zhuhai elder brother's prompt is resolved by judging whether the string length is the same as the character byte length to determine whether there are double byte characters
System. out. println (s1.getBytes (). length = s1.length ())? "S1 has no Chinese characters": "s1 has Chinese characters ");
System. out. println (s2.getBytes (). length = s2.length ())? "S2 has no Chinese characters": "s2 has Chinese characters ");
System. out. println (s3.getBytes (). length = s3.length ())? "S3 has no Chinese characters": "s3 has Chinese characters ");

OUTPUT:
S1 has Chinese Characters
S2 has no Chinese Characters
S3 has Chinese characters //
This way, we can determine whether a string contains double-byte characters. However, it is a bit difficult to accurately determine whether a string contains Chinese characters, we know that many characters in other countries are double-byte in Unicode.
Therefore, we need to further determine how to determine the encoding range of Chinese characters. I used this method, that is, the notepad now outputs the characters between 0 and, we can see that the first Chinese character is '1' and the last one is '?? '(I don't know it now). It's much easier to judge Chinese characters. For example, we can compare the encoding range of characters, finally, I will give you some results. The Chinese characters are basically concentrated in the range of [20901,], with a total of Chinese characters (if it's a little less, it's just how much you know)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.