String.split() in Java — How It Works and Pitfalls
Fast recall
Quick revision
Core answer
String.split(regex) splits the string around matches of the given regular expression and returns a String[]. The argument is a regex, not a plain string — special characters like ., |, +, *, ? must be escaped. By default, trailing empty strings are removed from the result.Whether you know that split() takes a regex, understand the escaping requirement for special chars, and know the trailing-empty-string behaviour.
Calling split('.') to split by dot — period is a regex metacharacter that matches ANY character. Every character becomes a delimiter and you get an array of empty strings. Must use split('\\.') to escape.
Mention the two-arg form split(regex, limit) — with limit -1 it keeps trailing empty strings; with a positive limit it caps the number of pieces. Also: Java 8 added String.join() as the inverse operation.
Interview-ready guide
Interview answer
The one thing that trips people up is that split() doesn't take a plain separator — it takes a regular expression. Most of the time you don't notice, because a comma or a space means nothing special in regex, so split(",") just works.
The trouble starts with characters that are metacharacters. The classic is splitting a filename on the dot: "report.2024.pdf".split(".") doesn't give you three pieces — . in regex matches any character, so it matches everything and you get back an empty array. The fix is to escape it, split("\\."); same story with the pipe, plus, and star. If the separator comes from user input or config and you can't be sure, wrap it in Pattern.quote() so it's treated literally.
The other behavior to know is trailing empties. "a,,b,,".split(",") returns three elements, not five — Java silently drops the empties at the end. When that matters, pass -1 as the limit to keep them, or a positive number to cap how many pieces you get with the remainder left unsplit in the last slot. The inverse operation is String.join(), added in Java 8. My rule is simple: if the separator is a literal character, always escape it or quote it — never trust that a character is harmless in regex.
String.split() in Java — How It Works and Pitfalls
Direct Answer
String.split(regex) splits the string around matches of the given regular expression and returns a String[]. The argument is a regex, not a plain string — special characters like ., |, +, *, ? must be escaped. By default, trailing empty strings are removed from the result.
Code Example
public class SplitDemo {
public static void main(String[] args) {
// Basic split — comma, no regex issue
String csv = "Alice,Bob,Carol";
String[] names = csv.split(",");
System.out.println(names.length); // 3
System.out.println(names[0]); // Alice
// PITFALL 1: dot is a regex metacharacter — matches ANY character
String filename = "report.2024.pdf";
String[] buggy = filename.split("."); // WRONG — splits on every char
System.out.println(buggy.length); // 0 — all matches, all empty!
String[] correct = filename.split("\\."); // escape the dot
System.out.println(correct.length); // 3
System.out.println(correct[0]); // report
// PITFALL 2: pipe | is regex OR — must escape it too
String data = "a|b|c";
// data.split("|") — WRONG: | means 'or nothing' in regex, splits every char
String[] fixed = data.split("\\|");
System.out.println(fixed.length); // 3
// PITFALL 3: trailing empty strings removed by default
String csv2 = "a,,b,,";
String[] parts = csv2.split(",");
System.out.println(parts.length); // 3 — trailing empties dropped
String[] all = csv2.split(",", -1); // -1 keeps all including trailing empty strings
System.out.println(all.length); // 5
// Limit argument — cap the number of pieces
String line = "one:two:three:four";
String[] limited = line.split(":", 2);
System.out.println(limited[0]); // one
System.out.println(limited[1]); // two:three:four — rest unsplit
}
}
Common Mistakes & Pitfalls
- Splitting on
.literally —split(".")treats the dot as regex "any character", matches every position, and returns an empty array. The literal-dot split issplit("\\."). - Leaving
|,+,*,(,[unescaped — all are regex metacharacters with their own meaning. Escape each with\\, or wrap the whole separator inPattern.quote(sep). - Expecting trailing empties to survive —
"a,,b,,".split(",")drops the trailing empties and returns 3 elements, not 5. Pass-1as the limit to keep them all. - Splitting in a hot loop —
split()compiles the regex on every call. For repeated parsing, precompile once withPattern.compile(regex)and reuse itssplit(). - Assuming leading empties are dropped too — only trailing empties are removed; a leading delimiter still yields an empty first element.
Check your understanding
Answer these without looking back at the article.
- How do you split a string by whitespace in Java?
- What is Pattern.quote() and when do you use it?
- What is String.join() and how does it relate to split()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.