1. Introduction
We all know that splitting a string is a very common task. However, we often split using just one delimiter.
In this tutorial, weβll discuss in detail different options for splitting a string by multiple delimiters.
2. Splitting a Java String by Multiple Delimiters
In order to show how each of the solutions below performs splitting, weβll use the same example string:
String example = "Mary;Thomas:Jane-Kate";
String[] expectedArray = new String[]{"Mary", "Thomas", "Jane", "Kate"};
2.1. Regex Solution
Programmers often use different regular expressions to define a search pattern for strings. Theyβre also a very popular solution when it comes to splitting a string. So, letβs see how we can use a regular expression to split a string by multiple delimiters in Java.
First, we donβt need to add a new dependency since regular expressions are available in the java.util.regex package. We just have to define an input string we want to split and a pattern.
The next step is to apply a pattern. A pattern can match zero or multiple times. To split by different delimiters, we should just set all the characters in the pattern.
Weβll write a simple test to demonstrate this approach:
String[] names = example.split("[;:-]");
Assertions.assertEquals(4, names.length);
Assertions.assertArrayEquals(expectedArray, names);
Weβve defined a test string with names that should be split by characters in the pattern. The pattern itself contains a semicolon, a colon, and a hyphen. When applied to the example string, weβll get four names in the array.
2.2. Guava Solution
Guava also offers a solution for splitting a string by multiple delimiters. Its solution is based on a Splitter class. This class extracts the substrings from an input string using the separator sequence. We can define this sequence in multiple ways:
- as a single character
- a fixed string
- a regular expression
- a CharMatcher instance
Further on, the Splitter class has two methods for defining the delimiters. So, letβs test both of them.
Firstly, weβll add the Guava dependency:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>32.1.3-jre</version>
</dependency>
Then, weβll start with the on method: public static Splitter on(Pattern separatorPattern)
It takes the pattern for defining the delimiters for splitting. First, weβll define the combination of the delimiters and compile the pattern. After that, we can split the string.
In our example, weβll use a regular expression to specify the delimiters:
Iterable<String> names = Splitter.on(Pattern.compile("[;:-]")).split(example);
Assertions.assertEquals(4, Iterators.size(names.iterator()));
Assertions.assertIterableEquals(Arrays.asList(expectedArray), names);
The other method is the onPattern method: public static Splitter onPattern(String separatorPattern)
The difference between this and the previous method is that the onPattern method takes the pattern as a string. There is no need to compile it like in the on method. Weβll define the same combination of the delimiters for testing the onPattern method:
Iterable<String> names = Splitter.onPattern("[;:-]").split(example);
Assertions.assertEquals(4, Iterators.size(names.iterator()));
Assertions.assertIterableEquals(Arrays.asList(expectedArray), names);
In both tests, we managed to split the string and get the array with four names.
Since weβre splitting an input string with multiple delimiters, we can also use the anyOf method in the CharMatcher class:
Iterable<String> names = Splitter.on(CharMatcher.anyOf(";:-")).split(example);
Assertions.assertEquals(4, Iterators.size(names.iterator()));
Assertions.assertIterableEquals(Arrays.asList(expectedArray), names);
This option comes only with the on method in the Splitter class. The outcome is the same as for the previous two tests.
2.3. Apache Commons Solution
The last option weβll discuss is available in the Apache Commons Lang 3 library.
Weβll start by adding the Apache Commons Lang dependency to our pom.xml file:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.13.0</version>
</dependency>
Next, weβll use the split method from the StringUtils class:
String[] names = StringUtils.split(example, ";:-");
Assertions.assertEquals(4, names.length);
Assertions.assertArrayEquals(expectedArray, names);
We only have to define all the characters weβll use to split the string. Calling the split method will divide the example string into four names.
3. Conclusion
In this article, weβve seen different options for splitting an input string by multiple delimiters. First, we discussed a solution based on regular expressions and plain Java. Later, we showed different options available in Guava. Finally, we wrapped up our examples with a solution based on the Apache Commons Lang 3 library.
