Tuesday, December 10, 2013

Java: How to Split Strings In Java

Splitting Strings in Java is easy, because there is a split() method implemented in the class String, which means every string object can be split with a pattern provided. Here is an example:


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


Most of the time, split() is used to break well formatted messages. For example, "One|Two|Three" is well formatted with values in-between every backslash. What if the message is like this: "One||Three"?


String text = "One||Three";

String[] tokens = text.split("\\|"); 

for (String token : tokens){
    System.out.println(token);
}

One

Three


In this example, you can see that the text was split into {"One", "", "Three"}. If you want the result to be {"One", "Three"}, you need to modify the regular expression.


String text = "One||Three";

String[] tokens = text.split("[\\|]+"); 

for (String token : tokens){
    System.out.println(token);
}

One
Three


As you can see, if you are familiar with regular expressions, split() is a very useful tool to split texts with complicated patterns.

No comments: