Split String in Java using a given delimiter
This post will explore several ways to split the string in Java using a given delimiter.
1. Using String.split() method
The standard solution is to use the split() method provided by the String class. It takes the delimiting regular expression as an argument and returns an array of strings.
Please note that we need to escape a few characters in some cases, which happens to be a special character in the regex. For example, a dot(.), pipe(|) or dollar($).
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.Arrays; class Main { public static void main(String[] args) { String str = "A-B-C"; String[] result = str.split("-"); System.out.println(Arrays.toString(result)); } } |
Output:
[A, B, C]
2. Using Guava’s Splitter Class
Another good alternative is to use the Splitter class from the Guava library, as shown below. Note that this returns an Iterable instead of an array of strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.base.Splitter; class Main { public static void main(String[] args) { String str = "A-B-C"; String delim = "-"; Iterable<String> result = Splitter.on(delim).split(str); System.out.println(result); } } |
Output:
[A, B, C]
3. Using splitAsStream() method
From Java 8 onwards, we can also use the splitAsStream() method, which returns the stream of strings computed by splitting the input around matches of the given pattern.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.util.Arrays; import java.util.regex.Pattern; class Main { public static void main(String[] args) { String str = "A-B-C"; String delim = "-"; String[] result = Pattern.compile(delim) .splitAsStream(str) .toArray(String[]::new); System.out.println(Arrays.toString(result)); } } |
Output:
[A, B, C]
4. Using Apache Commons Lang
We can also leverage the StringUtils.split() method of Apache Commons Lang library, which splits the string into an array using a given separator.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import org.apache.commons.lang3.StringUtils; import java.util.Arrays; class Main { public static void main(String[] args) { String str = "A-B-C"; String delim = "-"; String[] result = StringUtils.split(str, delim); System.out.println(Arrays.toString(result)); } } |
5. Using StringTokenizer Class
The StringTokenizer is a legacy class that allows breaking a string into tokens using a set of delimiters. This solution is not recommended in the new code.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.StringTokenizer; class Main { public static void main(String[] args) { String str = "A-B-C"; String delim = "-"; StringTokenizer result = new StringTokenizer(str, delim); while (result.hasMoreTokens()) { System.out.println(result.nextToken()); } } } |
Output:
A
B
C
That’s all about splitting Java String using a given delimiter.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)