String text = "One;Two;Three";
//Splitting the text on semi-colons
//Note that an array of String is returned.
String[] tokens = text.split(";");
//Loop over the array to display the result.
for (String token : tokens){
System.out.println(token);
}
One
Two
Three
Since the split() function accepts regular expression as the parameter, if you want to split on, for example, a backslash "|", then you need to escape it.
String text = "One|Two|Three";
//Splitting the text on backslashes
//Note that there are 2 '\'.
String[] tokens = text.split("\\|");
//Loop over the array to display the result.
for (String token : tokens){
System.out.println(token);
}
One
Two
Three
String text = "One||Three";
String[] tokens = text.split("\\|");
for (String token : tokens){
System.out.println(token);
}
One
Three
String text = "One||Three";
String[] tokens = text.split("[\\|]+");
for (String token : tokens){
System.out.println(token);
}
One
Three
No comments:
Post a Comment