Java Programming Tutorials

Java programming tutorials with many code examples!

Java Split String in various ways

Java Split String

In this tutorial we’re going to show how to split string effectively in Java, consider alternative ways and what are differences between them.


Notice that all String.split() versions invoke Pattern.compile() underneath, so they are not a good fit for tight loops.

String.split(regexp, positive limit)

Limit is applied n-1 times = 3 – 1 = 2 times:

String s = "Stefan Banach";

System.out.print("s.split(\"a\", 3): ");
System.out.println(Arrays.toString(s.split("a", 3)));

Array will be not greater than n, here 3:

s.split("a", 3): [Stef, n B, nach]

String.split(regexp, negative limit)

Non-positive limit = apply as many times as possible:

String s = "Stefan Banach";

System.out.print("s.split(\"a\", -1): ");
System.out.println(Arrays.toString(s.split("a", -1)));

// non-positive = apply as many times as possible:
System.out.print("s.split(\"h\", -1): ");
System.out.println(Arrays.toString(s.split("h", -1)));

Notice empty trailing string in the second split:

s.split("a", -1): [Stef, n B, n, ch]
s.split("h", -1): [Stefan Banac, ]

String.split(regexp, zero limit)

Zero (0) = apply as many times as possible and remove trailing empty strings:

String s = "Stefan Banach";

System.out.print("s.split(\"h\", 0): ");
System.out.println(Arrays.toString(s.split("h", 0)));

Notice no empty trailing string as in s.split(“h”, -1) case:

s.split("h", 0): [Stefan Banac]


It’s the same as limit = 0:

String s = "Stefan Banach";

System.out.print("s.split(\"h\"): ");
s.split("h"): [Stefan Banac]

Using Pattern.compile(regexp) (for loops)

When you have to split string in a loop using the same regular expression then it’s more efficient to compile the pattern upfront using java.util.regex.Pattern and then use it to split strings, like here:

String s = "Stefan Banach";

Pattern pattern = Pattern.compile("h");

System.out.print("Pattern.compile(\"h\").split(s): ");
// Split in a loop without Pattern.compile() cost:

Result is the same as from String.split(), but more efficient:

Pattern.compile("h").split(s): [Stefan Banac]

Using StringTokenizer

In cases when a string should be split using characters java.util.StringTokenizer may be a good alternative. It allows to scan long strings one token at a time:

String s = "Stefan Banach";

StringTokenizer tokenizer = new StringTokenizer(s, "h");
System.out.print("StringTokenizer(s,\"h\"): ");

Here result is the same as in s.split(“h”):

StringTokenizer(s,"h"): [Stefan Banac]
Was it helpful? Share!