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
๐ŸŒ
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 are an experienced Java ... regex in Java. It will not only teach you basics of regex but also empower you with how to use them effectively. Also here is a nice regular expression cheat sheet to remember most used special characters in regex: Here is our sample program to show you how to split a String by dot (.) in ...
๐ŸŒ
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....
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ split-string-by-dot-in-java
Split String by Dot (.) in Java
September 28, 2022 - 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 ...
๐ŸŒ
Revisit Class
revisitclass.com โ€บ home โ€บ how to split the string by dot in java?
How to split the string by dot in Java? -
May 14, 2022 - public class StringSplit { public static void main(String[] args) { //Input string String name = "www.revisitclass.com"; //Mentioned dot with double backslash in regex String splitStr[] = name.split("\\."); //Print each string from the array for (String eachStr : splitStr){ System.out.println(eachStr); } } }
๐ŸŒ
Code2care
code2care.org โ€บ home โ€บ java โ€บ how to split on string in java with regular expressions by dot.
How to Split on String in Java with Regular Expressions by Dot. | Code2care
July 7, 2023 - package org.code2care.example; import java.util.regex.Matcher; import java.util.regex.Pattern; public class JavaSplitOnDotStringExample2 { public static void main(String[] args) { String sentence = "1.2.3.4.5.6.7.8"; Pattern pattern = Pattern.compile("(\\d+)"); Matcher matcher = pattern.matcher(sentence); //Print them out while (matcher.find()) { String splittedString = matcher.group(1); System.out.print(splittedString+"\t"); } } } Output:
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ split-string-with-dot-in-java
Split String with Dot (.) in Java
December 26, 2024 - The split() method of Java String is used to divide a string into an array of substrings. This method takes a string in the form of a regular expression as a parameter, searches the currently existing string with the given pattern, and splits ...
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2016 โ€บ 02 โ€บ 2-ways-to-split-string-with-dot-in-java-using-regular-expression.html
Javarevisited: 2 ways to Split String with Dot (.) in Java using Regular Expression? Examples
In this example, you will find what works, what doesn't work, and why. The examples are pretty much similar to splitting String by any delimiter, with only a focus on using the correct regular expression because the dot is a special character in Java's regular expression API.
๐ŸŒ
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.
๐ŸŒ
EyeHunts
tutorial.eyehunts.com โ€บ home โ€บ java string split method | regex with space, comma, dot examples
Java string split Method | Regex Space, Comma, Dot Example - EyeHunts
May 17, 2021 - A Java string Split methods have used to get the substring or split or char form String. Split method works on Regex matched case, if matched then split ...
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.
๐ŸŒ
Mkyong
mkyong.com โ€บ home โ€บ java โ€บ how to split a string in java
How to split a string in Java - Mkyong.com
February 9, 2022 - Period or dot . ... P.S Both the closing square bracket ] and closing curly brace } is an ordinary character, no need to escape. If we want to split a string using one of the special regex characters as the delimiter, we must escape it. For example, if we want to split a string by a vertical bar or pipe symbol | (special regex character), which means or in the regex, we must escape the | symbol using one of the following ways:
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ string โ€บ java string split() : splitting by one or multiple delimiters
Java String split() : Splitting by One or Multiple Delimiters
October 12, 2023 - For using the multiple delimiters, the regular expression should define a character class that includes all the delimiters we want to split by. The regular expression must be a valid pattern and we must remember to escape special characters if necessary. String str = "how-to-do.in.java"; String[] strArray1 = str.split("-"); //[how, to, do.in.java] - 3 tokens String[] strArray2 = str.split("-|\\."); //[how, to, do, in, java] - 5 tokens
๐ŸŒ
Javadevnotes
javadevnotes.com โ€บ java-string-split-dot-examples
Java String Split Dot Or Period Examples - JavaDevNotes
March 16, 2015 - Here is how to split a String with dots but all tokens are trimmed on both sides. public class TestConsole { public static void main(String[] args) { String sampleString = "A . B . C"; String[] items = sampleString.split("\\s*\\.\\s*"); int itemIndex = 1; for (String item : items) { System.out.println(itemIndex + ".