I am wondering if I am going about splitting a string on a .
the right way? My code is:
String[] fn = filename.split(\".\");
return fn[0];
Using ApacheCommons it's simplest:
File file = ...
FilenameUtils.getBaseName(file.getName());
Note, it also extracts a filename from full path.
the String#split(String) method uses regular expressions. In regular expressions, the "." character means "any character". You can avoid this behavior by either escaping the "."
filename.split("\\.");
or telling the split method to split at at a character class:
filename.split("[.]");
Character classes are collections of characters. You could write
filename.split("[-.;ld7]");
and filename would be split at every "-", ".", ";", "l", "d" or "7". Inside character classes, the "." is not a special character ("metacharacter").
As DOT( . ) is considered as a special character and split method of String expects a regular expression you need to do like this -
String[] fn = filename.split("\\.");
return fn[0];
In java the special characters need to be escaped with a "\" but since "\" is also a special character in Java, you need to escape it again with another "\" !
Split uses regular expressions, where '.' is a special character meaning anything. You need to escape it if you actually want it to match the '.' character:
String[] fn = filename.split("\\.");
(one '\' to escape the '.' in the regular expression, and the other to escape the first one in the Java string)
Also I wouldn't suggest returning fn[0] since if you have a file named something.blabla.txt
, which is a valid name you won't be returning the actual file name. Instead I think it's better if you use:
int idx = filename.lastIndexOf('.');
return filename.subString(0, idx);
split
takes a regex as argument. So you should pass "\."
instead of "."
because "."
is a metacharacter in regex.
I see only solutions here but no full explanation of the problem so I decided to post this answer
You need to know few things about text.split(delim)
. split
method:
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
["" "" "" ""]
, 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.
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
split("[.]")
split("\\Q.\\E")
Pattern.LITERAL
flagsplit(Pattern.quote("."))
and let regex do escaping for you.