Java
String Split
Programming
Code Syntax
Dot Separator

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:

java
String text = "one.two.three";
String[] parts = text.split(".");

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:

java
1String text = "one.two.three";
2String[] parts = text.split("\\.");
3
4for (String part : parts) {
5    System.out.println(part);
6}

This prints:

text
one
two
three

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.

java
1import java.util.regex.Pattern;
2
3public class Main {
4    public static void main(String[] args) {
5        String version = "1.2.3";
6        String[] parts = version.split(Pattern.quote("."));
7
8        for (String part : parts) {
9            System.out.println(part);
10        }
11    }
12}

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.

java
1String value = "a.b.";
2String[] parts = value.split("\\.");
3
4System.out.println(parts.length);

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:

java
1String value = "a.b.";
2String[] parts = value.split("\\.", -1);
3
4System.out.println(parts.length);
5for (String part : parts) {
6    System.out.println("[" + part + "]");
7}

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.

java
1String version = "2.15.7";
2String[] rawParts = version.split("\\.");
3int major = Integer.parseInt(rawParts[0]);
4int minor = Integer.parseInt(rawParts[1]);
5int patch = Integer.parseInt(rawParts[2]);
6
7System.out.println(major + " " + minor + " " + patch);

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.

java
1String filename = "archive.tar.gz";
2int lastDot = filename.lastIndexOf('.');
3
4String name = filename.substring(0, lastDot);
5String extension = filename.substring(lastDot + 1);
6
7System.out.println(name);
8System.out.println(extension);

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.split uses 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 indexOf or lastIndexOf when you only need one separator boundary.

Course illustration
Course illustration

All Rights Reserved.