๐Ÿ”ค Strings & Text ยท Beginner

Splitting & joining in Java

split takes a regex; String.join and Collectors.joining.

๐Ÿงฉ The mysterySplit "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;  // 3

The 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]
๐Ÿ”ฎ Predict it

The vanishing pieces

What does this print?

String[] p = "1.2.3".split(".");
System.out.println(p.length);
  1. 3
  2. 5
  3. 0
Show the answer

0! The regex . matches every character, so every character becomes a separator and all the pieces between them are empty.

๐Ÿค” Think first

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.

๐Ÿ”ฎ Predict it

Middle vs end

What does this print?

String[] p = "x,,y,,".split(",");
System.out.println(p.length);
System.out.println(Arrays.toString(p));
  1. 5 [x, , y, , ]
  2. 3 [x, , y]
  3. 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 trap

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 the real world

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

  1. split takes a regex: use "\\." for a literal dot
  2. Trailing empty strings are removed from split's result
  3. String.join(sep, parts) glues parts with a separator
  4. Collectors.joining(sep, prefix, suffix) does the same for streams
๐Ÿคฏ Did you know?

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);
  1. 3
  2. 5
  3. 0
  4. 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);
  1. 2026-10-03 a, b
  2. -2026-10-03- a, b
  3. 2026-10-03 [a, b]
  4. 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.

Next: a report built with += takes minutes for 100,000 lines. One class makes it instant.