How to Split String in Java

I think you will agree that split a string in Java is a very popular task.

Usually, you need to cut a string delimiter and parse other string parts into an array or list.

I’ll explain to you 5 the most popular methods how to split a string in Java.

How to Split String in Java: Methods Overview

There are 3 split functions in Java Core and 2 split methods in Java libraries.

So what is a difference?

  • If you need to split a string into an array – use  String.split(s).
  • If you need to split a string into collection (list, set or whatever you want) – use  Pattern.compile(regex).splitAsStream(delimiter).
  • StringTokenizer is a legacy class. Don’t know why it’s not deprecated, but use the 1st method instead.  Anyway, you should know how it’s working, that’s why I provided code examples.
  • com.google.common.base.Splitter was popular before Java 8 release. It provides API more or less similar to  Pattern.compile(regex).splitAsStream(delimiter).
  • org.apache.commons.lang3.StringUtils.split(s) provides a built-in null check, so sometimes it’s more convenient way instead of  String.split(s).

So let’s take a deeper look.

String.split(regex)

Splits a string into an array of strings by regex delimiter.

Method signature:

Parameter String regex is the delimiting or regular expression.

Example:

Output:

Reference:

The extended method with a parameter int limit is present as well.

It uses limit to indicate how many rows should be returned.

Method signature:

Example:

Output:

Reference:

Tweet This MethodClick To Tweet

Pattern.compile(regexp).splitAsStream(input)

This function allows splitting a string into a stream and process stream to List or Set, or even Map.

Example:

Output:

Since: Java 8

Reference:

Tweet This MethodClick To Tweet

StringTokenizer

java.util.StringTokenizer is a legacy class and I do not recommend to use it anymore.

This class is maintained for compatibility reasons.

The main question for me is: why it’s not deprecated?!

To parse the string, you must organize a loop, using hasMoreTokens() and nextToken() methods.

Example:

Output:

Reference:

Tweet This MethodClick To Tweet

Google Guava Splitter

Google Guava is an open-source set of common libraries for Java, mainly developed by Google engineers.

If you want to use Guava you should add maven dependency:

Splitter has rich API, it can: omit empty strings, trim results, set limit, map to list etc.

The separator can be specified as a single character, fixed string, regular expression or CharMatcher instance.

On my opionion, since Java 8 it can be replaced with Java Stream API (Pattern.compile(regex).spliteToStream(input)).

Example:

Output:

Reference:

Tweet This MethodClick To Tweet

Apache Commons StringUtils

Library Apache Commons has its own utility class to work with strings – StringUtils.

Add maven dependency:

StringUtils split method looks similar to String.split(s):

The difference is: StringUtils.split() method contains a built-in null check.

Example:

Output:

Reference:

Tweet This MethodClick To Tweet

Popular Split Regular Expressions

I think it would be useful to provide some frequently asked regex to split a string.

Split String by Space

Split String by Whitespace

\\s means to split a string by whitespace character (in ASCII it’s tab, line feed, form feed, carriage return, and space; in Unicode, also matches no-break spaces, next line, and the variable-width spaces).

Split String by Comma

Split String by Slash

Split String by Backslash

First of all, it’s impossible to have a string like this in Java:  "1\2\3".

It should be escaped like this:  "1\\2\\3".

Or you can read it from file and Java will escape string for you.

Delimiter should be escaped as well.

Split String by Question Mark

Split String by Dollar Sign

Split String by Colon

Split String by Dot

Split String By Plus

Split String by Pipe

Split String By Tab

Split String by Multiple Delimiters

Tweet Split Regular ExpressionsClick To Tweet

Any questions? Please, ask me.

1 Star2 Stars3 Stars4 Stars5 Stars
Loading...
Tags:
Scroll Up