Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

2 Comments
Oldest
Newest