Java string split with . (dot)
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To split a Java string on a literal dot, you cannot use "." directly. String.split takes a regular expression, and in regular expressions a dot means "any character," so you need to escape it and pass "\\." instead.
Why split(".") Does Not Work
This looks reasonable at first:
But it does not mean "split on periods." In regex syntax, . matches any single character, so the pattern is far broader than intended. The result is usually confusing because Java is not treating the dot as ordinary text.
The correct version is:
This prints:
Why the Escape Needs Two Backslashes
There are two layers involved:
- regex syntax needs
\.to mean a literal dot - Java string syntax needs
\\to produce one backslash character
So the Java string literal becomes "\\.".
Once that rule clicks, many other split patterns become easier to reason about. You are not just escaping for Java; you are escaping for both Java string parsing and the regex engine.
Using Pattern.quote as an Alternative
If you do not want to think about regex escaping at all, you can ask Java to quote the separator for you.
For a simple literal dot, "\\." is shorter and more common. Pattern.quote becomes more attractive when the delimiter is configurable or contains several special regex characters.
Be Aware of Trailing Empty Strings
split has another behavior that surprises people: trailing empty strings are discarded by default.
This prints 2, not 3, because the empty string after the final dot is dropped.
If you need to keep trailing empty fields, pass a negative limit:
Now the trailing empty token is preserved. This matters in parsing tasks where empty fields are meaningful, such as simple delimited formats or version-like strings with optional suffix positions.
Common Use Cases
Splitting on dots appears in several everyday cases:
- parsing package or class names
- splitting version numbers such as
1.2.3 - separating domain-like identifiers
- handling dotted configuration keys
The exact post-processing depends on the case. For version numbers, you might convert each part to int. For package names, you usually keep them as strings.
When to Avoid split
If you only need the position of the first or last dot, indexOf or lastIndexOf may be simpler and faster than regex-based splitting.
split is best when you actually want multiple pieces, not just one boundary.
Common Pitfalls
The most common mistake is forgetting that split uses regular expressions and passing "." as if it were plain text. Another is misunderstanding the double escape and trying to use "\.", which is not a valid Java string literal. Developers also get surprised by dropped trailing empty strings when parsing data with optional final fields. Finally, split is sometimes used when indexOf would be simpler because the code only needs one separator position.
Summary
- '
String.splituses regex, so a literal dot must be escaped.' - Use
"\\."to split on an actual period. - Use
Pattern.quote(".")when you want a literal-safe alternative. - Pass a negative limit if trailing empty strings must be preserved.
- Use
indexOforlastIndexOfwhen you only need one separator boundary.

