Quick revision
The short answer
Both methods take regular expressions, not literal text. In split("."), the dot means any character, so every character is a separator, every piece between them is empty, and split removes trailing empty strings, leaving an empty array; split("\\.") or split(Pattern.quote(".")) fixes it. In replaceAll("$price", value), the $ is an end-of-input anchor, so the pattern never matches and nothing is replaced, and a $ or backslash in the replacement value is read as a group reference, which can throw. For literal text, use String.replace(CharSequence, CharSequence), which replaces every occurrence literally despite its name, or quote both sides with Pattern.quote and Matcher.quoteReplacement.
What to remember
In this answer, we saw that split, replaceAll, replaceFirst and matches treat their first argument as a regular expression. split(".") makes every character a separator, producing only empty strings, which the default limit then removes as trailing empties, so the result is an empty array; split("\\.") or Pattern.quote fixes it, and a limit of -1 keeps trailing fields. replaceAll("$price", value) never matches because $ is an end anchor, and its replacement treats $ and backslash as group references, which fails on data such as $5. For literal text, String.replace replaces every occurrence, and Pattern.quote with Matcher.quoteReplacement make regex methods literal.
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.
Both methods take regular expressions, not literal text. In split("."), the dot means any character, so every character is a separator, every piece between them is empty, and split removes trailing empty strings, leaving an empty array; split("\\.") or split(Pattern.quote(".")) fixes it. In replaceAll("$price", value), the $ is an end-of-input anchor, so the pattern never matches and nothing is replaced, and a $ or backslash in the replacement value is read as a group reference, which can throw. For literal text, use String.replace(CharSequence, CharSequence), which replaces every occurrence literally despite its name, or quote both sides with Pattern.quote and Matcher.quoteReplacement.
Why this question is asked
These two bugs come from the same root: some of String's most-used methods quietly treat their arguments as regular expressions. The names do not warn us, so a developer who wants to split on a dot or replace a placeholder writes code that looks right and fails in a confusing way, an empty array or a replacement that does not happen. Interviewers ask this debugging question to check that we know which String methods are regex-based and how to make them literal. In this answer, we will recap which methods use regex, walk through the split result step by step, fix it, explain both problems with replaceAll, and finish with a table of the literal alternatives.
1A Quick Recap: Which String Methods Use Regex
String.split(String), replaceAll(String, String), replaceFirst(String, String) and matches(String) all interpret their first argument as a regular expression. In a regex, several characters have special meaning: the dot matches any character, $ and ^ are anchors for the end and start, and characters such as *, +, ?, |, parentheses and brackets have their own roles.
In contrast, String.replace(CharSequence, CharSequence), replace(char, char), indexOf and contains work with literal text. The naming is unfortunate: replace sounds like it replaces one occurrence and replaceAll sounds like the literal version that replaces all, but in fact both replace every occurrence, and the difference is regex versus literal.
2Why split(".") Returns an Empty Array
The pattern "." matches any single character, so in "a.b.c" every one of the five characters is a separator, including the letters. Between and around those five separators there are six pieces, and all of them are empty strings.
- Compile "." as a regexdot = any character
- Find separatorsa, ., b, ., c: all 5 characters match
- Collect pieces between them6 empty strings
- Limit is 0 (default)?YesRemove trailing empty stringsNoKeep them
- Resultempty array, length 0
5 steps in order. Play them, or step through them one at a time.
Then split applies its second rule: with the default limit, trailing empty strings are removed from the result. Since every piece is empty, all of them are trailing, and they are all removed, leaving an array of length zero. The second call shows the hidden pieces by passing a negative limit, which keeps them.
3Fixing the Split
We need the dot to be a literal character. In a regex, a backslash escapes a special character, and in a Java string literal the backslash itself must be escaped, so the pattern is written "\\.". Alternatively, Pattern.quote wraps any text so it is matched literally, which is the clearest choice when the separator comes from a variable.
Notice the trailing-empty rule again in the last two lines. When parsing data such as CSV rows where an empty last field matters, we pass -1 as the limit so that the number of fields stays correct.
4Why replaceAll("$price", value) Fails
There are two separate problems, one on each side. In the pattern, $ is an anchor meaning end of input. So "$price" means end of input, followed by the letters price, which can never match because nothing follows the end. replaceAll finds no match and returns the text unchanged, with no error.
The second problem is in the replacement string. In replaceAll, $1, $2 and so on refer to captured groups, and a backslash escapes the next character. So a price value such as $5 is read as a reference to group 5, which does not exist, and a value ending in $ or a backslash is an illegal reference. This is the nasty part: the bug depends on the data, so it passes tests with a value like 10 and fails in production with a value that contains a dollar sign. Matcher.quoteReplacement escapes those characters so the value is inserted literally.
5Choosing the Right Method
The practical rule is simple. When we mean literal text, we use the literal methods: replace for replacements, indexOf and contains for searching, and split with Pattern.quote when there is no literal split method. When we really need regex, we compile the pattern once, quote any user-supplied parts with Pattern.quote, and pass user-supplied replacement values through Matcher.quoteReplacement.
| Method | First argument | Replaces | Special in replacement |
|---|---|---|---|
replace(CharSequence, CharSequence) | literal | every occurrence | nothing |
replace(char, char) | literal | every occurrence | nothing |
replaceAll(String, String) | regex | every match | $ and backslash |
replaceFirst(String, String) | regex | first match | $ and backslash |
split(String) | regex | (splits) | n/a |
6Follow-up questions
Why does split(",") work even though it takes a regex?
Because a comma has no special meaning in a regex, so the pattern matches a literal comma. String.split even has a fast path that avoids the regex engine for a single non-special character. The problems start with characters such as the dot, the pipe and the dollar sign, which are special.
What does split("|") return?
The pipe means alternation between two empty patterns, so it matches the empty string at every position and the input is split into single characters, "a|b" becoming [a, |, b]. The fix is the same: split("\\|") or split(Pattern.quote("|")).
Is there a performance difference between replace and replaceAll?
Since JDK 9, replace(CharSequence, CharSequence) no longer uses regex internally and is a simple, fast literal search. replaceAll compiles a pattern on each call, so for literal replacements replace is both correct and faster.
7Conclusion
In this answer, we saw that split, replaceAll, replaceFirst and matches treat their first argument as a regular expression. split(".") makes every character a separator, producing only empty strings, which the default limit then removes as trailing empties, so the result is an empty array; split("\\.") or Pattern.quote fixes it, and a limit of -1 keeps trailing fields. replaceAll("$price", value) never matches because $ is an end anchor, and its replacement treats $ and backslash as group references, which fails on data such as $5. For literal text, String.replace replaces every occurrence, and Pattern.quote with Matcher.quoteReplacement make regex methods literal.
Open with
Both come from the same thing: split and replaceAll take regular expressions, not plain text. In split with a dot, the dot means any character, so every character in a.b.c, letters included, is a separator.
Then, if they want more
That leaves six pieces, all empty strings, and split with the default limit removes trailing empty strings, and since they are all trailing, you get an empty array. You can see the six empties if you pass a limit of minus one.
The fix is to escape the dot, so backslash backslash dot in Java source, or Pattern.quote of the dot, which I prefer when the separator is a variable. In replaceAll with dollar price there are two problems.
In the pattern, dollar is the end-of-input anchor, so the regex means end of input followed by price, which can never match, and nothing is replaced, silently. And in the replacement, dollar and backslash are special: dollar 5 means group 5, so a value like a price with a dollar sign throws No group 5 or Illegal group reference.
That one is nasty because it depends on the data. For a literal placeholder, the right method is String.replace with two CharSequences. Despite the name, it replaces every occurrence, just literally, and since JDK 9 it does not use regex at all. If I really need replaceAll, I wrap the pattern with Pattern.quote and the value with Matcher.quoteReplacement.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- 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 "Why does "a.b.c".split(".") return an empty array, and why does...": https://scrivio-interview-library.netlify.app/java/strings/09a9c37632/