Splitting & joining in Java
split takes a regex; String.join and Collectors.joining.
"a.b.c" on "." and Java returns an array with zero elements. Not three, not five โ zero. Something about that dot is very special.Cutting a String
split(sep) cuts a String at every separator and returns a String[] of the pieces.
String[] p = "red,green,blue".split(",");
// [red, green, blue]
int n = p.length; // 3The argument is a regex
Surprise: split takes a regular expression, not plain text. In regex, . means "any character" and | means "or". To split on the literal symbol, escape it: "\\." (one backslash for the regex, doubled for Java) โ or use Pattern.quote(".").
"a.b".split("\\."); // [a, b]
"a|b".split("\\|"); // [a, b]The vanishing pieces
What does this print?
String[] p = "1.2.3".split(".");
System.out.println(p.length);350
Show the answer
0! The regex . matches every character, so every character becomes a separator and all the pieces between them are empty.
But why zero, not six empty pieces?
If every character is a separator, there should be lots of empty Strings. Where did they go?
Think about it, then reveal the answer
split drops trailing empty Strings. Since *every* piece is empty, they're all "trailing" โ so they all vanish, leaving an empty array. Empty pieces in the middle are kept; only the ones at the end disappear.
Middle vs end
What does this print?
String[] p = "x,,y,,".split(",");
System.out.println(p.length);
System.out.println(Arrays.toString(p));5 [x, , y, , ]3 [x, , y]2 [x, y]
Show the answer
3 and [x, , y]. The empty piece between the two commas stays; the empty pieces after the last y are trailing, so split removes them.
Gluing pieces back
String.join(sep, ...) puts the separator only between elements, never at the ends. It accepts varargs or any Iterable like a List. At the end of a stream, use Collectors.joining(delimiter, prefix, suffix).
String.join("-", "2026", "10", "03");
// "2026-10-03"
Stream.of("x", "y", "z")
.collect(Collectors.joining(
"|", "[", "]"));
// "[x|y|z]"The pipe trap
"a|b".split("|") returns [a, |, b]. In regex, | means OR โ with nothing on either side, it matches the empty gap between every character. Fix it with split("\\|"). And no, split('|') with a char doesn't exist: it won't compile.
In real projects
Splitting an IP address with ip.split(".") and getting an empty array is a rite of passage for Java developers. For real CSV files (quoted commas, line breaks inside fields) use a CSV library โ split(",") can't handle "Smith, John".
Key takeaways
- split takes a regex: use "\\." for a literal dot
- Trailing empty strings are removed from split's result
- String.join(sep, parts) glues parts with a separator
- Collectors.joining(sep, prefix, suffix) does the same for streams
split has a second parameter: a negative limit like split(",", -1) keeps trailing empty Strings. So "a,b,,".split(",", -1) has 4 pieces instead of 2.
Practice questions
What does this print?
String[] parts = "a.b.c".split(".");
System.out.println(parts.length);- 3
- 5
- 0
- 1
Check your answer
0. "." matches every character, so only empty strings remain between them. split drops trailing empty strings, so the result is empty. Use split("\\.").
What does this print?
String d = String.join("-", "2026", "10", "03");
String e = String.join(", ", List.of("a", "b"));
System.out.println(d);
System.out.println(e);- 2026-10-03 a, b
- -2026-10-03- a, b
- 2026-10-03 [a, b]
- 20261003 a,b
Check your answer
2026-10-03 a, b. String.join places the separator only between elements, never at the ends. It accepts varargs or any Iterable such as a List.