Use of the split function in Java, the pluplit Function
The split function in Java is used to split a string by a specified character (string) or regular expression, and the result is returned in the form of a string array;
For example:
String str = "1234 @ abc ";
String [] a = str. split ("@");
System. out. println ("processing result:" + a [0] + "," + a [1]); // The output result is: 1234, abc.
For separated characters (strings), it is usually common and common, and there is no problem;
However, for some special characters, if the character (string) is exactly a part of the regular expression, it must be escaped before it can be used,
These characters include |, +, *, ^, $,/, |, [,], (,),-,., \, because they are part of a regular expression, if you want to use the character itself, these characters must be escaped to represent itself;
For example:
If you want to use | vertical bars to separate a character, because | itself is a part of a regular expression, you need to use \ to escape the character, and this \ is exactly the character of the regular expression, therefore, we need to use one \, so we need two \\.
String str = "5678 | XYZ ";
String [] B = str. split ("\ |"); // note that two \ instead of one \
System. out. println ("processing result:" + B [0] + "," + B [1]); // The output result is: 5678, XYZ
Let's take a look:
String str = "5678 | XYZ ";
String [] B = str. split ("|"); // use it directly. | this character is part of a regular expression,
String x = "processing result :";
For (int I = 0; I <B. length; I ++ ){
X = x + B [I] + ",";
}
System. out. println (x); // The output result is: 5, 6, 7, 8, |, X, Y, Z,
Java sample code: http://www.eyesourcecode.com/f/JavaSourceCode/1