Quick revision
The short answer
They return 4, 6, 3, [a, b, c] and x-y-z. split() takes a regex and, with the default limit of 0, removes trailing empty strings, while a negative limit keeps them; a leading empty string is always kept unless the very first match has zero width. Because the argument is a regex, \\d+ splits on runs of digits. To join with a prefix and suffix, we use new StringJoiner(", ", "[", "]") or Collectors.joining(", ", "[", "]").
What to remember
In this answer, we saw that split() treats its argument as a regex and that its limit decides what happens at the ends: with the default 0, trailing empty strings are removed, so a,b,,c,, gives 4 pieces, while -1 keeps them and gives 6. A leading empty string is kept, giving 3 for ,a,b, and \d+ splits on runs of digits to give [a, b, c]. String.join gives x-y-z, and StringJoiner or Collectors.joining add a prefix and a suffix.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
They return 4, 6, 3, [a, b, c] and x-y-z. split() takes a regex and, with the default limit of 0, removes trailing empty strings, while a negative limit keeps them; a leading empty string is always kept unless the very first match has zero width. Because the argument is a regex, \\d+ splits on runs of digits. To join with a prefix and suffix, we use new StringJoiner(", ", "[", "]") or Collectors.joining(", ", "[", "]").
Why this question is asked
split() looks like the simplest method in the String class, and that is exactly why its rules surprise people. Parsing a line of input, reading a configuration value or handling a list sent as text all depend on knowing what happens to empty values at the start and end, and on remembering that the argument is a regular expression. Interviewers use a puzzle like this to see whether we know those rules. In this answer, we will recap how split() works, go through each expression and why it gives its result, and then look at three ways to join strings, including with a prefix and suffix.
1A Quick Recap of split(regex, limit)
split(String regex) is a shortcut for split(regex, 0). It finds every match of the regular expression and returns the pieces of text between the matches. The limit argument controls two things: how many pieces we get, and whether empty pieces at the end survive. A positive limit n means at most n pieces, so the last piece contains the rest of the string untouched. A negative limit means as many pieces as there are, all kept. A limit of zero, the default, also means as many as there are, but then removes empty strings from the end of the result.
That last rule, dropping trailing empties only at limit zero, explains most of the surprises in the question.
2Expressions 1 and 2: Trailing Empty Strings
The string a,b,,c,, has five commas. Splitting on each comma gives six pieces: a, b, an empty string between the two adjacent commas, c, an empty string between the last two commas, and an empty string after the final comma.
| Piece | Text | limit 0 | limit -1 |
|---|---|---|---|
| 0 | a | kept | kept |
| 1 | b | kept | kept |
| 2 | empty | kept: not at the end | kept |
| 3 | c | kept | kept |
| 4 | empty | removed: trailing | kept |
| 5 | empty | removed: trailing | kept |
With the default limit 0, the two empty strings at the end are removed, but the empty string in the middle stays, because only trailing empties are dropped. So we get 4. With limit -1 nothing is removed and we get all 6. This matters whenever empty fields carry meaning, for example a record with an optional last column: with the default split, a line with an empty last field looks like a line with a missing column. When we parse fixed formats, we usually want split(",", -1) and then check the count.
3Expression 3: A Leading Empty String Is Kept
The string ,a,b starts with a comma. The text before that first comma is empty, so the first piece is an empty string, then a, then b.
So leading empties are kept but trailing ones are not, an asymmetry worth remembering. There is one exception since Java 8: when the match at the very start has zero width, as with the empty regex in the second line, no leading empty string is produced. The last two lines are also worth knowing. Splitting an empty string returns an array holding that empty string, so its length is 1, not 0. A lone comma produces two empty pieces that are both trailing, so the result is empty.
4Expression 4: The Argument Is a Regex
The argument to split is a regular expression, not a literal string. \\d+ in Java source is the regex \d+, which means one or more digits. So the string is cut at the 1 and at the 22, and the digits themselves disappear because they are the separators.
The second line shows why the plus matters: without it, each digit is a separate separator, and the empty text between the two 2s becomes an empty piece. The third line is a common bug: a dot means any character, so every piece is empty and, because they are all trailing, the result is an empty array. For a literal separator, we escape it or use Pattern.quote("."). For a single character that is not special in regex, such as a comma, split takes a fast path that avoids the regex engine; other patterns are compiled on every call, so in hot loops we keep a precompiled Pattern and call pattern.split(line).
5Expression 5 and Other Ways to Join
String.join(delimiter, elements) is the reverse of split: it puts the delimiter between the elements, so String.join("-", List.of("x", "y", "z")) is x-y-z. It cannot add a prefix or suffix, and that is where the other two ways come in.
StringJoiner is useful when we add elements one at a time, for example inside a loop with conditions, and setEmptyValue lets us choose what an empty joiner prints; without it, an empty joiner prints just the prefix and suffix, []. Collectors.joining uses a StringJoiner internally and fits naturally at the end of a stream, for example after mapping objects to their names. String.join itself also uses a StringJoiner, so all three produce the same result for the same inputs.
6Follow-up questions
Why does split("|") not split on the pipe character?
Because | is the alternation operator in a regex, so the pattern means empty string or empty string. An empty match occurs at every position, so the string is split between every pair of characters: "a|b".split("|") returns [a, |, b], with the pipe as a piece of its own. To split on a literal pipe we escape it, split("\\|"), or use split(Pattern.quote("|")). The same applies to other regex characters such as the dot, the plus, the star, brackets and the dollar sign.
How do we split on a line break that may be \n or \r\n?
We use the regex \R, written "\\R" in Java source, which matches any Unicode line break sequence, including \r\n as a single break. Even better, String.lines() returns a lazy stream of lines and treats \n, \r and \r\n as terminators without a regex. Splitting on "\n" alone leaves a stray \r at the end of every line from a Windows file.
Should we use split to parse CSV?
Only for simple, controlled formats where values can never contain the separator. Real CSV allows quoted fields that contain commas, doubled quotes and even line breaks, and split knows nothing about quoting, so columns shift. For real CSV we use a parser such as Apache Commons CSV, FastCSV or Jackson's CSV module.
7Conclusion
In this answer, we saw that split() treats its argument as a regex and that its limit decides what happens at the ends: with the default 0, trailing empty strings are removed, so a,b,,c,, gives 4 pieces, while -1 keeps them and gives 6. A leading empty string is kept, giving 3 for ,a,b, and \d+ splits on runs of digits to give [a, b, c]. String.join gives x-y-z, and StringJoiner or Collectors.joining add a prefix and a suffix.
Open with
They return 4, 6, 3, the array a, b, c, and x-y-z. split takes a regex and a limit, and the default limit is zero, which means split as many times as possible and then remove empty strings at the end.
Then, if they want more
So a,b,,c,, gives a, b, an empty string, c, and the two trailing empties are dropped: 4. With a negative limit nothing is dropped, so we get 6, which is what I use for formats where an empty last field means something. A leading empty string is kept, so comma a comma b gives an empty string, a, b: 3.
The only exception, since Java 8, is a zero-width match at the very start. The fourth one reminds us the argument is a regex: backslash d plus splits on runs of digits, so a1b22c gives a, b, c, and the digits disappear as separators.
That is also why splitting on a dot or a pipe goes wrong unless you escape it or use Pattern.quote. String.join puts the delimiter between elements, x-y-z.
For a prefix and suffix I would use a StringJoiner, constructed with comma-space, an opening bracket and a closing bracket, then add the elements; or on a stream, Collectors.joining with the same three arguments. Both give the list in brackets, and StringJoiner also lets you set what an empty result prints.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 8
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What do these return, and why?": https://scrivio-interview-library.netlify.app/java/strings/6f13c20efc/