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.
๐ŸŒ
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 ยท
๐ŸŒ
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.
๐ŸŒ
Revisit Class
revisitclass.com โ€บ home โ€บ how to split the string by dot in java?
How to split the string by dot in Java? - - Revisit Class
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); } } }
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ 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 ...
๐ŸŒ
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....
๐ŸŒ
Techie Delight
techiedelight.com โ€บ home โ€บ java โ€บ split a string in java using dot, dollar, or question mark as a delimiter
Split a String in Java using dot, dollar, or question mark as a delimiter
July 7, 2026 - This post will explore different ways to split a string in Java using the dot (.), pipe (|) or dollar ($) or question mark (?) as a delimiter. The standard solution is to use the split() method provided by the String class.
Find elsewhere
๐ŸŒ
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 ยท
๐ŸŒ
W3docs
w3docs.com โ€บ home โ€บ code snippets โ€บ java โ€บ java string split with "." (dot)
Java string split with "." (dot) | W3docs
To split a string in Java using a dot (.) as the delimiter, you can use the split method of the String class.
๐ŸŒ
Codemia
codemia.io โ€บ home โ€บ knowledge hub โ€บ java string split with . (dot)
Java string split with . (dot) | Codemia
January 26, 2025 - IntroductionTo split a Java string on a literal dot, you cannot use "." directly. String.split takes a regular expression, and in regular expressions a dot...
๐ŸŒ
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...
๐ŸŒ
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.
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.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ java string.split()
Java.String.split() | Baeldung
July 15, 2026 - 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"; ...
๐ŸŒ
YouTube
youtube.com โ€บ intact abode
How to Split a String with delimiter dot "." in Java - Intact Abode - YouTube
- Java Swings consultants and developers - Jaspersoft Studio Reports consultants and developersPing me on Skype ID : jysuryam@outlook.comHow to Split a Strin...
Published: December 4, 2018
Views: 1K
๐ŸŒ
YouTube
youtube.com โ€บ watch
Java String Split with Dot (.)
To learn more, please visit the YouTube Help Center: https://www.youtube.com/help
๐ŸŒ
Cach3
tutorialspoint.com.cach3.com โ€บ How-to-use-Java-String-split-method-to-split-a-string-by-dot.html
How to use Java String.split() method to split a string by dot
February 12, 2018 - How to use Java String.split() ... matches of the given regular expression To split a string by dot pass a dot by placing double escape characters as ExampleLive Demopublic class Data public static......
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 771194 โ€บ java โ€บ string-split-method-work
Why does my string.split(.) method not work? (Beginning Java forum at Coderanch)
March 19, 2023 - If you look at the Javadoc for ... 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 \ character....