Split a String in Java using dot, dollar, or question mark as a delimiter
This post will explore different ways to split a string in Java using the dot (.), pipe (|) or dollar ($) or question mark (?) as a delimiter.
1. Using String.split() method
The standard solution is to use the split() method provided by the String class. It takes a regular expression as a delimiter and returns a string array.
|
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 ip = "127.0.0.1"; String[] result = ip.split("."); System.out.println(Arrays.toString(result)); // output: [] } } |
If we run the above code, it doesn’t work as expected. This is because the split() method takes a regular expression, and the dot is a special character that matches any character in the regex. We can avoid this behavior by creating a regex that will represent a dot. There are several ways to do this, which are covered below in detail:
⮚ Using an escape character
We can make the above code work by escaping the dot character. An escape character invokes an alternative interpretation on the following characters of a string. In Java, the \ (backslash) is used to escape special characters. Note that Java follows the two backslash-escape styles.
|
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 ip = "127.0.0.1"; String[] result = ip.split("\\."); System.out.println(Arrays.toString(result)); // output: [127, 0, 0, 1] } } |
⮚ Wrapping dot between \Q and \E
Another plausible way of escaping is using \Q to backslash all subsequent special characters and \E to end the expression.
|
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 ip = "127.0.0.1"; String[] result = ip.split("\\Q.\\E"); System.out.println(Arrays.toString(result)); // output: [127, 0, 0, 1] } } |
⮚ Character Class
We can also use Character class in Java for escaping the dot (.), as shown below:
|
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 ip = "127.0.0.1"; String[] result = ip.split("[.]"); System.out.println(Arrays.toString(result)); // output: [127, 0, 0, 1] } } |
⮚ Pattern.quote()
Another good alternative is to use the Pattern.quote(), which returns a literal pattern string for the specified string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; import java.util.regex.Pattern; class Main { public static void main(String[] args) { String ip = "127.0.0.1"; String[] result = ip.split(Pattern.quote(".")); System.out.println(Arrays.toString(result)); // output: [127, 0, 0, 1] } } |
2. Using Guava’s Splitter Class
We can use the Splitter class from the Guava library, as shown below, which returns an Iterable.
|
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 ip = "127.0.0.1"; Iterable<String> result = Splitter.on(".").split(ip); System.out.println(result); // output: [127, 0, 0, 1] } } |
3. 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 specified delimiter.
|
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 ip = "AB.X.Y"; String[] result = StringUtils.split(ip, "."); System.out.println(Arrays.toString(result)); // output: [127, 0, 0, 1] } } |
4. Using StringTokenizer Class
The StringTokenizer is a legacy class that allows breaking a string into tokens using a delimiter, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; class Main { public static void main(String[] args) { String ip = "127.0.0.1"; StringTokenizer st = new StringTokenizer(ip, "."); List<String> result = new ArrayList<>(); while (st.hasMoreTokens()) { result.add(st.nextToken()); } System.out.println(result); // output: [127, 0, 0, 1] } } |
That’s all about splitting a Java String using dot, dollar, or question mark as a 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 :)