Today, when writing a program, it is necessary to use multiple if () {}else{} statements, to observe the conditions that need to be judged, to find that the conditions of judgment can be changed to the judgment of the last few Chinese characters, and to use regular expressions to judge the Chinese characters, and after writing, Think that you can use the Endwith () method of the string class in Java to match Chinese characters, such as
String str= "Hello World I like Programming";
if (Str.endwith ("Programming")) {
DoSomething ();
}else{
DoSomething ();
}
The following is the source code for the Endwith () method:
Public BooleanStartsWith (String paramstring,intparamint) { Char[] ArrayOfChar1 = This. Value; inti = This. Offset +Paramint; Char[] ArrayOfChar2 =Paramstring.value; intj =Paramstring.offset; intK =Paramstring.count; if((Paramint < 0) | | (Paramint > This. Count-k))return false; while(true) {k--; if(K < 0) Break; if(arrayofchar1[(i++)]! = arrayofchar2[(j + +)]) return false; } return true; } Public BooleanStartsWith (String paramstring) {returnStartsWith (paramstring, 0); } Public BooleanEndsWith (String paramstring) {returnStartsWith (Paramstring, This. Count-paramstring.count); }
The char type can store a Chinese character because the encoding used in Java is Unicode (no specific encoding is selected, directly using the character's number in the character set, which is the only way to unify),
A char type occupies 2 bytes byte (16 bit bit), so it is no problem to put a Chinese.
1: "Byte" is byte, "bit" is bit;
2:1 byte = 8 bit;
Char is 2 bytes in Java. Java uses unicode,2 bytes (16 bits) to represent one character.
Java uses Unicode to represent characters, and Unicode for Chinese characters is 2 bytes.
Regular Expressions Match Chinese:
Package test1; Import Java.util.regex.Pattern; Public class patterntest { publicstaticvoid main (string[] args) { = "Programming World Hello, I love Programming"; System.out.println (Pattern.compile ("[compilation] [process]"). Matcher (str). find ());} }
Str.matches ("[compilation]") is not used when a string is matched, or false if used.
Description of the return of matches (string regx) in the Java API: Returns True if and only if this string matches the given regular expression, that is, the regular expression you given needs to match the entire string to return true. Instead of using the matches (string regx) method, the current scenario is equivalent to finding a string that has no specific string.
Note: When writing a matching rule, the matching characters need to be matched separately by [].
Java Endwith () method and regular expression matching Chinese