Java determines whether it is a Chinese character or whether it has a Chinese character

Source: Internet
Author: User
Java checks whether it is a Chinese character and whether it has a Chinese character

This article is for reprinting others' articles

Java code

  1. PublicBooleanVd (String str ){
  2. Char[] Chars = Str. tochararray ();
  3. BooleanIsgb2312 =False;
  4. For(IntI = 0; I <chars. length; I ++ ){
  5. Byte[] Bytes = ("" + chars [I]). getbytes ();
  6. If(Bytes. Length = 2 ){
  7. Int[] Ints =NewInt[2];
  8. Ints [0] = bytes [0] & 0xff;
  9. Ints [1] = bytes [1] & 0xff;
  10. If(Ints [0]> = 0x81 & ints [0] <= 0xFE & ints [1]> = 0x40 & ints [1] <= 0xFE) {
  11. IsGB2312 =True;
  12. Break;
  13. }
  14. }
  15. }
  16. ReturnIsgb2312;
  17. }

First, import java. util. regex. Pattern and java. util. regex. Matcher.
The two packages are followed by the code

Determines whether it is a number.

Java code

  1. PublicBooleanIsnumeric (string Str)
  2. {
  3. Pattern pattern = pattern. Compile ("[0-9] *");
  4. Matcher isnum = pattern. matcher (STR );
  5. If(! Isnum. Matches ()){
  6. ReturnFalse;
  7. }
  8. ReturnTrue;
  9. }
  10. Java. Lang. character. isdigit (CH [0])
Java code
  1. PublicStaticVoidMain (string [] ARGs ){
  2. IntCount = 0;
  3. String regEx = "[\ u4e00-\ u9fa5]";
  4. // System. out. println (regEx );
  5. String str = "Chinese fdas ";
  6. // System. out. println (str );
  7. Pattern p = Pattern. compile (regEx );
  8. Matcher m = p. matcher (str );
  9. While(M. find ()){
  10. For(IntI = 0; I <= m. groupCount (); I ++ ){
  11. Count = count + 1;
  12. }
  13. }
  14. System. out. println ("Total" + count + "count ");
  15. }

-------------------------------------------------------------------

Method for Determining whether a java String contains Chinese Characters

Java uses Unicode-encoded char variables in the range of 0-65535 unsigned values, which can represent
65536 characters. Basically, all the characters on Earth can be included. In reality, we want to determine whether a character is a Chinese character, or whether the characters in a string contain Chinese characters to meet business needs
Evaluate, the String class has such a method to get its character length (). See the following example,

Java code
  1. String s1 = "I am a Chinese ";
  2. String s2 = "imchinese ";
  3. String s3 = "im Chinese ";
  4. System. out. println (s1 + ":" +NewString (s1). length ());
  5. System. out. println (s2 + ":" +NewString (s2). length ());
  6. System. out. println (s3 + ":" +NewString (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

Java code
  1. System. out. println (s1.getBytes (). length = s1.length ())? "S1 has no Chinese characters": "s1 has Chinese characters ");
  2. System. out. println (s2.getBytes (). length = s2.length ())? "S2 has no Chinese characters": "s2 has Chinese characters ");
  3. System. out. println (s3.getBytes (). length = s3.length ())? "S3 has no Chinese characters": "s3 has Chinese characters ");
Related Article

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.