The Split function in Java is used to split a string by a specified character (string) or regular, and the result is returned as an array of strings;
For example:
String str= "[email protected]";
String[] A = Str.split ("@");
SYSTEM.OUT.PRINTLN ("Processing result:" +a[0]+ "," +a[1]); The output is: processing result: 1234,ABC
For segmented characters (strings), usually common, ordinary, no problem;
But for some special characters, if the character (string) is exactly part of the regular, it needs to be escaped to be used.
These characters have | , + , * , ^ , $ , / , | , [ , ] , ( , ) , - , . , \ ET, because they are part of a regular expression, so if you want to use the character itself, these characters need to be escaped in order to represent itself;
For example:
Want to use | Vertical bar to split a character, because | itself is a part of the regular expression, so need \ to escape, because the escape using \, and this \ is exactly the character of the regular expression, so you have to use a \, so need two \ \.
String str= "5678| XYZ ";
String[] B = Str.split ("\\|"); Notice here with two \ \ instead of a \
SYSTEM.OUT.PRINTLN ("Processing result:" +b[0]+ "," +b[1]); The output is: processing result: 5678,xyz
Take a look again:
String str= "5678| XYZ ";
String[] B = Str.split ("|"); Note that the character is part of a regular expression, directly using |
String x= "processing result:";
for (int i=0;i<b.length;i++) {
X=x+b[i]+ ",";
}
SYSTEM.OUT.PRINTLN (x); The output is: processing result: 5,6,7,8,|,x,y,z,
Java Sample code: HTTP://WWW.EYESOURCECODE.COM/F/JAVASOURCECODE/1
The use of the split function in Java