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.
๐ŸŒ
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 - By using regular expressions and character classes in the regular expression, we can split a string based on multiple delimiters. The following Java program splits a string with multiple delimiters, a hyphen and a dot.
๐ŸŒ
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 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 \ ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ split-string-with-dot-in-java
Split String with Dot (.) in Java
December 26, 2024 - 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
๐ŸŒ
Quora
quora.com โ€บ How-do-I-split-a-String-at-using-split-method-in-Java
How to split a String at '.' using .split() method in Java - Quora
Answer (1 of 6): .split() takes a Regular Expression as a parameter. But if you want to split on a *special character you need to use the backslash \ or a character class [โ€œ โ€] to escape these characters. So in your case you would have .split(โ€œ\\.โ€) or .split(โ€œ[.]โ€) - Example below: [code]pub...
Find elsewhere
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ split a string in java
Split a String in Java | Baeldung
January 8, 2024 - String[] splitted = input.trim().split("\\s*,\\s*"); Here, trim() method removes leading and trailing spaces in the input string, and the regex itself handles the extra spaces around delimiter. We can achieve the same result by using Java 8 Stream features:
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 09 โ€บ split-string-by-dot-in-java
Split String by Dot (.) in Java
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.
๐ŸŒ
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....
๐ŸŒ
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)
January 26, 2014 - For example, in your case, if Java's input stream reader knew that you wanted to chuck out data that was in between the first 4 dots, it would have just chucked it out for you while it was reading the characters over the stream, right? You wouldn't even have to parse the String. By breaking ...
Top answer
1 of 4
22

I've written a quick and dirty benchmark test for this. It compares 7 different methods, some of which require specific knowledge of the data being split.

For basic general purpose splitting, Guava Splitter is 3.5x faster than String#split() and I'd recommend using that. Stringtokenizer is slightly faster than that and splitting yourself with indexOf is twice as fast as again.

For the code and more info see https://web.archive.org/web/20210613074234/http://demeranville.com/battle-of-the-tokenizers-delimited-text-parser-performance (original link is dead and corresponding site does not appear to exist anymore)

2 of 4
6

As @Tom writes, an indexOf type approach is faster than String.split(), since the latter deals with regular expressions and has a lot of extra overhead for them.

However, one algorithm change that might give you a super speedup. Assuming that this Comparator is going to be used to sort your ~100,000 Strings, do not write the Comparator<String>. Because, in the course of your sort, the same String will likely be compared multiple times, so you will split it multiple times, etc...

Split all the Strings once into String[]s, and have a Comparator<String[]> sort the String[]. Then, at the end, you can combine them all together.

Alternatively, you could also use a Map to cache the String -> String[] or vice versa. e.g. (sketchy) Also note, you are trading memory for speed, hope you have lotsa RAM

HashMap<String, String[]> cache = new HashMap();

int compare(String s1, String s2) {
   String[] cached1 = cache.get(s1);
   if (cached1  == null) {
      cached1 = mySuperSplitter(s1):
      cache.put(s1, cached1);
   }
   String[] cached2 = cache.get(s2);
   if (cached2  == null) {
      cached2 = mySuperSplitter(s2):
      cache.put(s2, cached2);
   }

   return compareAsArrays(cached1, cached2);  // real comparison done here
}
๐ŸŒ
BeginnersBook -
beginnersbook.com โ€บ home โ€บ java โ€บ java string split() method with examples
Java String split() Method with examples
December 1, 2024 - public class JavaExample { public static void main(String args[]) { String str = "helloxyzhixyzbye"; String[] arr = str.split("xyz"); for (String s : arr) System.out.println(s); } } ... You can Split string by space using \s+ regex. Input: "Text with spaces"; Output: ["Text", "with", "spaces"] ... Split string by pipe ( | ) character using \| regex. Input: "Text1|Text2|Text3"; Output: ["Text1", "Text2", "Text3"] ... You can split string by dot ( .
๐ŸŒ
Google Groups
groups.google.com โ€บ g โ€บ google-web-toolkit โ€บ c โ€บ C6BRx0MQ1z4
String.split ( "." ) not working?
October 6, 2006 - ... Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message ... No. Not a bug. You have to escape the escape character! :o) backslash-dot is invalid because Java doesn't need to escape the dot.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ split a string only on the first occurrence of delimiter
Split a String Only on the First Occurrence of Delimiter | Baeldung
August 7, 2025 - In this tutorial, weโ€™ll learn how to split a Java String only on the first occurrence of a delimiter using two approaches. Letโ€™s say we have a text file having each line as a string made up of two parts โ€“ the left part indicating a personโ€™s name and the right part indicating their greeting: Roberto "I wish you a bug-free day!" Daniele "Have a great day!" Jonas "Good bye!"
๐ŸŒ
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.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ String โ€บ split
String.prototype.split() - JavaScript | MDN
The split() method of String values takes a pattern and divides this string into an ordered list of substrings by searching for the pattern, puts these substrings into an array, and returns the array.
๐ŸŒ
Softwaretestingboard
softwaretestingboard.com โ€บ q2a โ€บ 329 โ€บ java-split-string-using-dot-does-not-work
java split string using . dot does not work - Software Testing Board Q&A
December 31, 2015 - I am trying to split string using Java split function but it does not split string. this.getClass().getName().split(".");