Java checks whether it is a Chinese character and whether it has a Chinese character
This article is for reprinting others' articles
Java code
- PublicBooleanVd (String str ){
- Char[] Chars = Str. tochararray ();
- BooleanIsgb2312 =False;
- For(IntI = 0; I <chars. length; I ++ ){
- Byte[] Bytes = ("" + chars [I]). getbytes ();
- If(Bytes. Length = 2 ){
- Int[] Ints =NewInt[2];
- Ints [0] = bytes [0] & 0xff;
- Ints [1] = bytes [1] & 0xff;
- If(Ints [0]> = 0x81 & ints [0] <= 0xFE & ints [1]> = 0x40 & ints [1] <= 0xFE) {
- IsGB2312 =True;
- Break;
- }
- }
- }
- ReturnIsgb2312;
- }
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
- PublicBooleanIsnumeric (string Str)
- {
- Pattern pattern = pattern. Compile ("[0-9] *");
- Matcher isnum = pattern. matcher (STR );
- If(! Isnum. Matches ()){
- ReturnFalse;
- }
- ReturnTrue;
- }
- Java. Lang. character. isdigit (CH [0])
Java code
- PublicStaticVoidMain (string [] ARGs ){
- IntCount = 0;
- String regEx = "[\ u4e00-\ u9fa5]";
- // System. out. println (regEx );
- String str = "Chinese fdas ";
- // System. out. println (str );
- Pattern p = Pattern. compile (regEx );
- Matcher m = p. matcher (str );
- While(M. find ()){
- For(IntI = 0; I <= m. groupCount (); I ++ ){
- Count = count + 1;
- }
- }
- System. out. println ("Total" + count + "count ");
- }
-------------------------------------------------------------------
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
- String s1 = "I am a Chinese ";
- String s2 = "imchinese ";
- String s3 = "im Chinese ";
- System. out. println (s1 + ":" +NewString (s1). length ());
- System. out. println (s2 + ":" +NewString (s2). length ());
- 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
- 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 ");