Different Ways to Split a String in Java

Learn to split or tokenize a string into an array. Splitting a string is a very common task, especially working on web applications when we have to pass data in CSV format or separate based on some other separator such $, # or another separator.

1. Using Plain Java

The String.split() method is the best and recommended way to split the strings. The tokens are returned in form of a string array that frees us to use it as we wish.

The following Java program splits a string with the delimiter comma. It is equivalent to splitting a CSV file.

String blogName = "how,to,do,in,java";
String[] tokenArray = blogName.split(",");    //["how", "to", "do", "in", "java"]

We need to modify the regex expression for any additional requirements. Such for ignoring the whitespaces around commas, we can use the pattern “\\s,\\s”.

String[] tokenArray = blogName.split("\\s*,\\s*");

2. Using Splitter

The String.split() is very straightforward simple API for simple usages. If we want to process the tokens post splitting but before concluding the final result, the Splitter class is the best.

  • Using Splitter makes the code more readable and reusable also. We create a Splitter instance and reuse it multiple times, thus helping achieve uniform logic splitting in the whole application.
  • Another benefit is that it also provided some useful methods while building the splitter itself, eliminating a lot of after-work after creating the tokens.

We can directly include Guava from the maven repository.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>17.0</version>
</dependency>

We can create a Splitter instance in a fluent manner:

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();

And now, use it anywhere in the code as we like. Note that we have the commas twice. The Splitter handles it well and does not include the empty token.

Splitter niceCommaSplitter = Splitter.on(',').omitEmptyStrings().trimResults();

Iterable<String> tokensList = niceCommaSplitter.split("how,to,do,in, ,java");   

tokensList.forEach(System.out::println); //"how", "to", "do", "in", "java"

3. Using StringUtils.split()

The Apache Commons Lang’s StringUtils class provides many useful methods to perform common operations on Strings, such as search, replace, reverse or check empty. All operations are null safe.

The StringUtils.split() is very similar to the above approach and also returns the String array output. The only benefit is that the code is faster.

Start with including the latest version of common-lang3 dependency.

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

The constructor to initialize the StringUtils takes the following parameters:

split(String str, String separatorChars, int max)
  • str – the String to parse, may be null.
  • separatorChars (Optional) – the characters used as the delimiters. The default value is whitespace.
  • max (Optional) – the maximum number of elements to include in the array. A zero or negative value implies no limit.

The following Java program using StringUtils splits a string by delimiter whitespace.

String[] tokens = StringUtils.split("how to do in java");

Assertions.assertArrayEquals(new String[]{"how", "to", "do", "in", "java"}, tokens);

Happy Learning !!

Comments

Subscribe
Notify of
guest
6 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode