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 - Example: The simplest way to split a string by dot is by using the split() method of the String class. Since the dot "." is a special character in regular expressions, it needs to be escaped with double backslashes "\\" to treat it as a literal dot.
๐ŸŒ
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
March 1, 2024 - If you want to split String on the dot you need to escape dot as \\. instead of just passing "." to the split() method. Alternatively, you can also use the regular expression [.] to split the String by a dot in Java.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ split-string-by-dot-in-java
Split String by Dot (.) in Java
September 28, 2022 - public class JavaExample{ public static void main(String args[]){ //String that contains dot characters String str = "Text1.Text2.Text3.Text4"; //split the given string by using dot (.) as delimiter String[] strArray = str.split("\\."); //prints substrings after split for(String s: strArray){ System.out.println(s); } } } Output: Related java guides: Split String by Comma ยท
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ 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().
๐ŸŒ
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....
๐ŸŒ
Blogger
self-learning-java-tutorial.blogspot.com โ€บ 2022 โ€บ 05 โ€บ how-to-split-string-by-dot-in-java.html
Programming for beginners: How to split a string by dot (.) in Java?
you can even use the [.] form to split the String by dot. String[] tokens2 = str.split("[.]"); SplitByDotUsingRegularExpressions.java ยท
Find elsewhere
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.
๐ŸŒ
ZetCode
zetcode.com โ€บ java โ€บ splitstring
Java split string - splitting strings in Java
To split a string by a dot, we need to escape it or use Pattern.quote. ... import java.util.Arrays; import java.util.regex.Pattern; void main() { String address = "127.0.0.1"; // String[] output = address.split("\\."); String[] output = address.split(Pattern.quote(".")); Arrays.stream(outp...
๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ split-string-dot-delimiter-java
How to Split a String by a Dot Delimiter in Java - CodingTechRoom
Mistake: Using split(".") instead of split("\.") to split on a dot. Solution: Always escape the dot by using double backslashes in String methods. Mistake: Not checking if the string contains a dot before accessing fn[0]. Solution: Always perform a check to avoid ArrayIndexOutOfBoundsException.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to split a string in java
How to split a string in Java - Mkyong.com
February 9, 2022 - package com.mkyong.string.split; import java.util.StringTokenizer; public class StringSplitStringTokenizer { public static void main(String[] args) { String test = "abc.def.123"; // the delimiter is a string, not regex, no need to escape the dot StringTokenizer token = new StringTokenizer(test, "."); while (token.hasMoreTokens()) { System.out.println(token.nextToken()); } } } ... Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities. ... how to split email id by two and how to set name of before @ i mean how to set recipients name of before @
๐ŸŒ
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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java string.split()
Java.String.split() | Baeldung
November 9, 2017 - For example, if we want to split on a dot (.), we need to escape it like this: \\. because the dot (.) is a special character in regular expressions: @Test void whenSplitWithDotDelimiter_thenGetExpectedArray() { String s = "www.example.com"; ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 771194 โ€บ java โ€บ string-split-method-work
Why does my string.split(.) method not work? (Beginning Java forum at Coderanch)
If you look at the Javadoc for String#split you will see that it takes a regex (regular expression), and the dot character is a metacharacter and has a special use/meaning (other metacharacter are: \^$?|*+(){[ . If you want to split on a literal dot, then you will need to escape it with a \ ...
๐ŸŒ
Codemia
codemia.io โ€บ knowledge-hub โ€บ path โ€บ java_string_split_with__dot
Java string split with . (dot)
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-split-a-string-with-dot
Java Program to split a string with dot
June 27, 2020 - String str = "Java is a programming language. James Gosling developed it."; We will now see how to split a string using the split() method. Include the delimiter as the parameter. ... Above, we have split the string with dot as you can see under the split methods parameter.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ the split method in java does not work on a dot .
The split method in Java does not work on a dot . | Codemia
July 30, 2025 - This article explores the nuances of using the split() method with a focus on handling the dot delimiter. In Java, the split() method is a part of the String class, enabling you to divide a string into an array of substrings based on a regular expression.
๐ŸŒ
YouTube
youtube.com โ€บ tutorials in hand
how to split string with delimiter in java like comma, dot, pipe, space - YouTube
In this video tutorial, we are going to learn about how to split string with delimiter in java? We may have delimiter like { , . | : ; " ? } etc.. Here we us...
Published ย  December 6, 2022
Views ย  309