Split String in Java

In this artcile, we see how to split string in java. Understand how to use special characters, regular expressions and check before peforming the split. Limit the number of parts in the split

In this artcile, we see how to split string in java. Understand how to use special characters, regular expressions and check before peforming the split. Limit the number of parts in the split

String split()

Use the method String#split().

String string = "004-034556";
String[] parts = string.split("-");
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556

Split Metacharacters

Note that split's argument is assumed to be a regular expression, so remember to escape special characters if necessary.

Meta character Description
\ Escape special character
^ Beginning of input. ^start of words
$ End of input. line ends$
* zero or more times. a*
+ one or more times. b+
? zero or one time. .*?
. Matches any single character except \n
| Alternate match either a or b for a|b
( Capture enclosed value as variable
) Capture enclosed value as variable
[ start any matching character in range
] end matching character in range [abc]
{ Match consecutive characters.a{3} aaa
} Capture enclosed value as variable

I personally use regex101 for testing reguglar expressions before applying pattern matching.

Split with dot

For instance, to split on a period/dot . (which means "any character" in regex), use either backslash \ to escape the individual special character like so split("\\."), or use character class [] to represent literal character(s) like so split("[.]"), or use Pattern#quote() to escape the entire string like so split(Pattern.quote(".")).

String[] parts = string.split(Pattern.quote(".")); // Split on the exact string.

Test before Split with String contains

To test beforehand if the string contains certain character(s), just use String#contains().

if (string.contains("-")) {
    // Split it.
} else {
    throw new IllegalArgumentException("String " + string + " does not contain -");
}

Note, this does not take a regular expression. For that, use String#matches() instead.

Retaining the Split character after split

If you'd like to retain the split character in the resulting parts, then make use of positive lookaround. In case you want to have the split character to end up in left hand side, use positive lookbehind by prefixing ?<= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?<=-)");
String part1 = parts[0]; // 004-
String part2 = parts[1]; // 034556

In case you want to have the split character to end up in right hand side, use positive lookahead by prefixing ?= group on the pattern.

String string = "004-034556";
String[] parts = string.split("(?=-)");
String part1 = parts[0]; // 004
String part2 = parts[1]; // -034556

Limit number of parts after split

If you'd like to limit the number of resulting parts, then you can supply the desired number as 2nd argument of split() method.

String string = "004-034556-42";
String[] parts = string.split("-", 2);
String part1 = parts[0]; // 004
String part2 = parts[1]; // 034556-42