491

Why does the second line of this code throw ArrayIndexOutOfBoundsException?

String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split(".")[0];

While this works:

String driveLetter = filename.split("/")[0];

I use Java 7.

2
  • 7
    Doesn't split use a regex string? In that case "." means any character. Commented Feb 12, 2013 at 12:53
  • 4
    ...and it's a DOUBLE backslash to delimit. Commented Dec 6, 2013 at 5:41

4 Answers 4

974

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Note that filename can contain multiple dots. One must use the last index of "." and use that to find the substring of the filename.
@saurabheights The question was not about a correct regex, but rather why there was a an ArrayIndexOutOfBoundsException. That said, you are incorrect: You don't need to know where the last dot is; you just need the right regex: filename.split("\\.(?=[^.]*$)"). This uses a look ahead to assert there are no dots anywhere in the input that follows the matching dot.
@emma you can delete them yourself via the “delete” link just beneath the question
A cleaner solution : str.split(Pattern.quote("."))[0]
To split by a dot, you actually need to add four backslashes such as:
|
141

The dot "." is a special character in java regex engine, so you have to use "\\." to escape this character:

final String extensionRemoved = filename.split("\\.")[0];

3 Comments

It is not a special character in Java. It's a special character in Java's regex engine.
I just wasn't very accurate in my response but I agree with you. thanks for the precision ;)
It's a distinction worth making. Also, I just realized that I messed up a bit myself; it is a special char in Java, but that's not why it's causing a problem here. Anyway.
35

This is because . is a reserved character in regular expression, representing any character. Instead, we should use the following statement:

String extensionRemoved = filename.split("\\.")[0];

Comments

21

I believe you should escape the dot. Try:

String filename = "D:/some folder/001.docx";
String extensionRemoved = filename.split("\\.")[0];

Otherwise dot is interpreted as any character in regular expressions.

1 Comment

A cleaner solution : str.split(Pattern.quote("."))[0]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.