Java Reference ยท Text, Formatting and Regex
Pattern.split()
Reference for Pattern.split() and String.split(): splitting on a regex, the limit argument, leading and trailing empty strings, lookaround splits and splitAsStream.
Pattern.split(CharSequence input) cuts the input around every match of the pattern and returns the pieces as a String[]. String.split(regex) is a shortcut that compiles the regex and calls the same logic, with a fast path for single-character separators that are not regex meta-characters. The matches themselves are not part of the result.
The rules around empty strings are the part that surprises people: trailing empty strings are removed by default, a leading empty string appears when the input starts with a separator, and a zero-width match at the very beginning never creates a leading empty element (Java 8 and later).
Pattern.compile("\\s*,\\s*").split("a , b,c"); // ["a", "b", "c"] "a1b22c".split("\\d+"); // ["a", "b", "c"]
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
String[] Pattern.split(CharSequence input) String[] Pattern.split(CharSequence input, int limit) Stream<String> Pattern.splitAsStream(CharSequence input) // Java 8 String[] Pattern.splitWithDelimiters(CharSequence input, int limit) // Java 21 String[] String.split(String regex) String[] String.split(String regex, int limit)
| Name | Description |
|---|---|
input |
The text to split. |
limit |
> 0: at most limit pieces, the last holding the rest. 0: no limit, trailing empty strings removed. < 0: no limit, trailing empty strings kept. |
regex |
For String.split, the pattern text. |
Returns: an array of the pieces. If there is no match, an array with the input as its only element. For an empty input the result is [""] (one empty string).
Throws: PatternSyntaxException for an invalid regex; NullPointerException for null.
Usage notes
- Escape the separator.
split("."),split("|")andsplit("$")use regex meta-characters. Escape them or wrap withPattern.quote. - Empty strings at the start remain.
",a".split(",")begins with"". Strip or filter the input first if that is not wanted. With whitespace usestrip()beforesplit("\\s+"). - A single space splits every space.
"a b".split(" ")contains an empty string; use" +"or"\\s+". splitWithDelimiters(Java 21) returns the pieces and the matched delimiters interleaved, a clean alternative to lookaround tricks.- Fast path.
String.splitavoids compiling a regex when the separator is one character that is not special, or an escaped one. Elsewhere compile aPatternonce and reuse it. - Performance and memory. The result array and all substrings are created eagerly;
splitAsStreamis lazy in the sense it produces elements as consumed but still scans the whole input. - Not a CSV parser. Quoted fields containing commas need a real parser.
- Old and simple alternatives:
StringTokenizer(no empty tokens) andScanner.useDelimiter.