How to Properly Split a String with a Dot Delimiter in Java

Question

Why does using `split()` with a dot (`.`) delimiter in Java cause an ArrayIndexOutOfBoundsException?

String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split(".")[0];

Answer

In Java, the `String.split()` method is used to divide a string into an array of substrings based on a specified delimiter. However, using a dot (`.`) as a delimiter can lead to unexpected behavior due to regular expressions' interpretation. Here’s why your code throws an `ArrayIndexOutOfBoundsException` and how to fix it, along with a working approach.

String filename = "D:/some folder/001.docx";
String[] parts = filename.split("\\."); // Correctly splitting by dot
String extensionRemoved = parts[0]; // Accessing the base filename

Causes

  • The dot character in regular expressions matches any character except line terminators, therefore `filename.split(".")` effectively splits the string into each character rather than by the dot.
  • If the filename does not contain a dot (.), the resulting array will be of size 1, and trying to access the first element (index 0) is not an issue, but if the dot is at the beginning or missing, no valid index for non-existent substrings is accessible.

Solutions

  • To split the string by a literal dot, escape the dot with a double backslash: `filename.split("\\.")` which will correctly interpret it as a literal dot in the string.
  • Alternatively, you can use `StringUtils` from Apache Commons Lang which simplifies the splitting of strings without dealing with regex quirks.

Common Mistakes

Mistake: Using an unescaped dot as a delimiter in split causing incorrect array length.

Solution: Always escape special characters when using them as delimiters in regex functions.

Mistake: Not checking the resultant array's length before accessing an index.

Solution: Always check array length to prevent accessing an invalid index.

Helpers

  • Java split string
  • ArrayIndexOutOfBoundsException
  • String.split() method
  • Java string manipulation
  • Java regex escape character

Related Questions

⦿How to Create a Two-Dimensional Array in Java: Syntax Explained

Learn the correct syntax for creating a twodimensional array in Java with examples and common mistakes.

⦿How to Execute JUnit 4 Test Methods in a Specific Order?

Learn how to run test methods in a specific order in JUnit 4. Explore solutions and best practices for test execution in Java.

⦿How to Convert a String to Long in Java?

Learn how to convert a String representation of a long value to an actual long type in Java with easytofollow methods and code examples.

⦿Understanding the Role of the 'static' Keyword in Java Classes

Learn how the static keyword operates within Java classes its implications and when to use static members effectively.

⦿Should I Use a Static or Instance Field for Jackson's ObjectMapper?

Explore whether to declare Jacksons ObjectMapper as a static field considering its thread safety and performance implications.

⦿How to Obtain Source JARs from Maven Repositories?

Learn how to find and download source JARs from Maven repositories effectively with our expert guide.

⦿How to Check the Orientation of an Android Phone: Landscape or Portrait

Learn how to determine if your Android device is in landscape or portrait mode with this detailed guide and code examples.

⦿How to Download and Save a File from the Internet Using Java

Learn how to easily download and save files from URLs in Java with stepbystep guidance and code examples.

⦿How to Declare a Gradle Variable Usable in Java?

Learn how to declare variables in Gradle and use them in Java code effectively.

⦿How to Determine If the Current Thread is Not the Main (UI) Thread in Programming

Learn how to check if the current thread is not the main UI thread in your application with clear stepbystep guidance and code examples.

© Copyright 2025 - CodingTechRoom.com