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 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 - 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 ...
🌐
GeeksforGeeks
geeksforgeeks.org β€Ί java β€Ί java-split-string-by-dot
Java - Split String by Dot (.) - GeeksforGeeks
July 23, 2025 - Explanation: In the above example, we loop through each character in the string. Every time we encounter a dot, the accumulated characters (part of the string) are printed, and the StringBuilder is reset for the next segment.
🌐
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 - One more reason for this struggle is the instead of just passing β€œ.” to the split() method\\. . Alternatively, you can also use the regular expression [.] split the String by a dot in Java. The dot is mostly used to get the file extension as shown in our example.
🌐
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) { ...
🌐
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 it at every occurrence of the pattern matched. ... public class Demo { public static void main(String[] args) { String str = "This is demo text.This is sample text!"; String[] res = str.split("[.]", 0); for(String myStr: res) { System.out.println(myStr); } } }
🌐
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.prin...
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?
Since β€˜.’ is a regular expression pre defined character class, we need to explicitly escape it using a backslash while splitting by dot. ... package com.sample.app.strings; public class SplitByDotUsingRegularExpressions { public static void main(String args[]) { String str = "hello.world.how.are.you"; String[] tokens1 = str.split("\\."); String[] tokens2 = str.split("[.]"); for (String token : tokens1) { System.out.println(token); } System.out.println("\n\n"); for (String token : tokens2) { System.out.println(token); } } } ... package com.sample.app.strings; import java.util.ArrayList; imp
🌐
TutorialsPoint
tutorialspoint.com β€Ί java-program-to-split-a-string-with-dot
Java Program to split a string with dot
June 27, 2020 - Let’s say the following is our string. 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.
🌐
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:
🌐
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:
🌐
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.
🌐
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.
🌐
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...
🌐
Medium
abdulrahmansmile786.medium.com β€Ί 2-ways-to-split-string-with-dot-in-java-using-regular-expression-with-examples-f94366a15d41
2 ways to Split String with Dot (.) in Java using Regular Expression with examples | by Indian Support | Medium
July 28, 2022 - One more reason for this struggle is the instead of just passing β€œ.” to the split() method\\. . Alternatively, you can also use the regular expression [.] split the String by a dot in Java. The dot is mostly used to get the file extension as shown in our example.
🌐
javaspring
javaspring.net β€Ί blog β€Ί java-split-string-on-dot
Java Split String on Dot: A Comprehensive Guide β€” javaspring.net
Therefore, if you want to split a string on the actual dot character, you need to escape it using two backslashes (\\.). This is because the backslash is also an escape character in Java strings, so you need to use two backslashes to represent ...
🌐
CodingTechRoom
codingtechroom.com β€Ί question β€Ί split-string-dot-delimiter-java
How to Split a String by a Dot Delimiter in Java - CodingTechRoom
When attempting to split a string on a dot (.), it's essential to understand that in Java's regular expressions, a dot is a special character that matches any character. To split a string accurately using a dot as a delimiter, you must escape the dot in your split method. Below are step-by-step instructions and a code example for achieving this successfully.
🌐
TutorialsPoint
tutorialspoint.com β€Ί How-to-use-Java-String-split-method-to-split-a-string-by-dot
Java - String split() Method
June 18, 2020 - This method has two variants and splits this string around matches of the given regular expression. Here is the syntax of this method βˆ’ Here is the detail of parameters βˆ’ This will produce the following result βˆ’