There is no need to set the delimiter by breaking it up in pieces like you have done.
Here is a complete program you can compile and run:
import java.util.Arrays;
public class SplitExample {
public static final String PLAYER = "1||1||Abdul-Jabbar||Karim||1996||1974";
public static void main(String[] args) {
String[] data = PLAYER.split("\|\|");
System.out.println(Arrays.toString(data));
}
}
If you want to use split with a pattern, you can use Pattern.compile
or Pattern.quote
.
To see compile
and quote
in action, here is an example using all three approaches:
import java.util.Arrays;
import java.util.regex.Pattern;
public class SplitExample {
public static final String PLAYER = "1||1||Abdul-Jabbar||Karim||1996||1974";
public static void main(String[] args) {
String[] data = PLAYER.split("\|\|");
System.out.println(Arrays.toString(data));
Pattern pattern = Pattern.compile("\|\|");
data = pattern.split(PLAYER);
System.out.println(Arrays.toString(data));
pattern = Pattern.compile(Pattern.quote("||"));
data = pattern.split(PLAYER);
System.out.println(Arrays.toString(data));
}
}
The use of patterns is recommended if you are going to split often using the same pattern. BTW the output is:
[1, 1, Abdul-Jabbar, Karim, 1996, 1974]
[1, 1, Abdul-Jabbar, Karim, 1996, 1974]
[1, 1, Abdul-Jabbar, Karim, 1996, 1974]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…