You need to escape the dot if you want to split on a literal dot:

String extensionRemoved = filename.split("\\.")[0];

Otherwise you are splitting on the regex ., which means "any character".
Note the double backslash needed to create a single backslash in the regex.


You're getting an ArrayIndexOutOfBoundsException because your input string is just a dot, ie ".", which is an edge case that produces an empty array when split on dot; split(regex) removes all trailing blanks from the result, but since splitting a dot on a dot leaves only two blanks, after trailing blanks are removed you're left with an empty array.

To avoid getting an ArrayIndexOutOfBoundsException for this edge case, use the overloaded version of split(regex, limit), which has a second parameter that is the size limit for the resulting array. When limit is negative, the behaviour of removing trailing blanks from the resulting array is disabled:

".".split("\\.", -1) // returns an array of two blanks, ie ["", ""]

ie, when filename is just a dot ".", calling filename.split("\\.", -1)[0] will return a blank, but calling filename.split("\\.")[0] will throw an ArrayIndexOutOfBoundsException.

Answer from Bohemian on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-split-string-by-dot
Java - Split String by Dot (.) - GeeksforGeeks
July 23, 2025 - After the loop ends, we print the last part of the string. We can use the StringTokenizer class for splitting strings by a dot. This method is considered outdated but still works well for splitting strings.
🌐
Blogger
javarevisited.blogspot.com › 2016 › 02 › 2-ways-to-split-string-with-dot-in-java-using-regular-expression.html
2 ways to Split String with Dot (.) in Java using Regular Expression? Examples
You can use the split() method of java.lang.String class to split a string based on the dot. Unlike comma, colon, or whitespace, a dot is not a common delimiter to join String, and that's why beginner often struggles to split a String by dot.
🌐
TutorialsPoint
tutorialspoint.com › split-string-with-dot-in-java
Split String with Dot (.) in Java
In Java, strings are one of the most commonly used data types for storing text. Sometimes, you may need to split a string based on a specific delimiter, such as a dot (.). Java provides powerful string manipulation methods like split(). Split() metho
🌐
BeginnersBook
beginnersbook.com › 2022 › 09 › split-string-by-dot-in-java
Split String by Dot (.) in Java
You can split a string by dot using the following regex inside the String.split() method. Here, we need to use double backslash before dot(.) to escape it else it would split the string using any character. ... public class JavaExample{ public static void main(String args[]){ //String that ...
🌐
Coderanch
coderanch.com › t › 627606 › java › segments-dot-separated-string
get the last two segments from a dot separated string (Beginning Java forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I have something like: xxx.x.x.x.x.33423.AMDAC-4 The number of dot separated segment in front of the string may vary, but it doesn't matter since I only need to extract the last two segments from the string (int this case : 33423 and AMDAC-4).
Find elsewhere
🌐
ZetCode
zetcode.com › java › splitstring
Java split string - splitting strings in Java
We split the string by the dash character; the split method returns an array of substrings split from the main string. Arrays.stream(output).forEach(part -> System.out.println(part)); We show the split parts to the console. ... A dot character has a special meaning in regular expression syntax.
🌐
W3Docs
w3docs.com › java
Java string split with "." (dot)
To split a string in Java using a dot (.) as the delimiter, you can use the split method of the String class.
Top answer
1 of 13
211

split() accepts a regular expression, so you need to escape . to not consider it as a regex meta character. Here's an example :

String[] fn = filename.split("\\."); 
return fn[0];
2 of 13
25

I see only solutions here but no full explanation of the problem so I decided to post this answer

Problem

You need to know few things about text.split(delim). split method:

  1. accepts as argument regular expression (regex) which describes delimiter on which we want to split,
  2. if delim exists at end of text like in a,b,c,, (where delimiter is ,) split at first will create array like ["a" "b" "c" "" ""] but since in most cases we don't really need these trailing empty strings it also removes them automatically for us. So it creates another array without these trailing empty strings and returns it.

You also need to know that dot . is special character in regex. It represents any character (except line separators but this can be changed with Pattern.DOTALL flag).

So for string like "abc" if we split on "." split method will

  1. create array like ["" "" "" ""],
  2. but since this array contains only empty strings and they all are trailing they will be removed (like shown in previous second point)

which means we will get as result empty array [] (with no elements, not even empty string), so we can't use fn[0] because there is no index 0.

Solution

To solve this problem you simply need to create regex which will represents dot. To do so we need to escape that .. There are few ways to do it, but simplest is probably by using \ (which in String needs to be written as "\\" because \ is also special there and requires another \ to be escaped).

So solution to your problem may look like

String[] fn = filename.split("\\.");

Bonus

You can also use other ways to escape that dot like

  • using character class split("[.]")
  • wrapping it in quote split("\\Q.\\E")
  • using proper Pattern instance with Pattern.LITERAL flag
  • or simply use split(Pattern.quote(".")) and let regex do escaping for you.
🌐
Medium
abdulrahmansmile786.medium.com › 2-ways-to-split-string-with-dot-in-java-with-examples-da04d32a752b
2 ways to Split String with Dot (.) in Java with examples | by Indian Support | Medium
July 28, 2022 - Unlike comma, colon, or whitespace, ... the regular expression. If you want to split String on the dot you need to escape dot as split a String by dot....