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.
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.
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];
Use myString.lastIndexOf(".") to get the index of the last dot.
For example, if you are sure that your input String contains at least one dot :
String name = myString.substring(0,myString.lastIndexOf("."));
If you want to use split, you need to escape the dot (split expects a regular expression).
List<String> strings = Arrays.asList(myString.split("\\."));
If you only need to remove the last part, you can use replaceAll and a regex:
myString = myString.replaceAll("\\.[^.]*$", "");
Explanation:
\\.looks for a dot[^.]*looks for 0 or more non dot characters$is the end of the string
Use the String method lastIndexOf(int ch).
int lastIndxDot = st.lastIndexOf('.');
st.substring(0, lastIndxDot); will be the substring you want. If it returned -1, then there is no '.' in the string.
EDIT:
for (Map.Entry < String, String > entry: newList.entrySet()) {
String getAfterDot = entry.getKey();
int lastIndxDot = getAfterDot.lastIndexOf('.');
if (lastIndxDot != -1) {
String beforeDot = getAfterDot.substring(0, lastIndxDot);
System.out.println(beforeDot);
}
}
Try this code it should work:
static HashMap < String, String > newList = new HashMap < > ();
public static void main(String[] args) throws FileNotFoundException, IOException {
String inputFile = "input";
BufferedReader brInput = new BufferedReader(new FileReader(inputFile));
String line = null;
while ((line = brInput.readLine()) != null) {
newList.put(line, "x");
}
for (Map.Entry < String, String > entry: newList.entrySet()) {
String getAfterDot = entry.getKey();
if (getAfterDot.contains(".")) {
String[] split = getAfterDot.split("\\.");
String beforeDot = "";
beforeDot = getAfterDot.substring(0, getAfterDot.lastIndexOf("."));
System.out.println(beforeDot);
}
}
}
I got this output with above code:
0
0
2
0.3.5
0.2
The file extension is the part after the last dot and therefore the following should work
String str = "Udemy.txt";
String fileName = str.substring(0, str.lastIndexOf("."));
String extension = str.substring(str.lastIndexOf(".") + 1);
Try this,
String fileName = "MyFile.xls";
int dotIndex = fileName.lastIndexOf(".");
String name = fileName.substring(0, dotIndex); // MyFile
String extension = fileName.substring(dotIndex); // .xls
// then you can change and re-construct the file name
You can split the string on the zero-width location between the last dot and the character that precedes the last dot:
(?=\.[^.]+$)
Demo
You need the regex to only match the last dot, while insuring there are no more dot following it.
To do that you match the dot, which needs escaping, so use \., then use a zero-width positive look-ahead to verify that the rest of the string doesn't have a dot, i.e. use (?=[^.]+$)
Alternatively, use a zero-width negative look-ahead to ensure it cannot find a dot, i.e. (?!.*\.)
I prefer the first, so use \.(?=[^.]+$), which in Java means "\\.(?=[^.]+$)"
This handles all delim values:
String str = "21.12.2015";
String delim = "."; // or "-" or "?" or ...
String[] st = str.split(java.util.regex.Pattern.quote(delim));
When you say split you are using delim as a regex pattern. It is treated differently. Please have a look to this regular expression.
But when you are using delim in sysout you are using it as string. the difference is obviuos
You can try this
int i = s.lastIndexOf(c);
String[] a = {s.substring(0, i), s.substring(i)};
It might be easier to just assume that files which end with a dot followed by alphanumeric characters have extensions.
int p=filePath.lastIndexOf(".");
String e=filePath.substring(p+1);
if( p==-1 || !e.matches("\\w+") ){/* file has no extension */}
else{ /* file has extension e */ }
See the Java docs for regular expression patterns. Remember to escape the backslash because the pattern string needs the backslash.
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];
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:
- accepts as argument regular expression (regex) which describes delimiter on which we want to split,
- if
delimexists at end oftextlike ina,b,c,,(where delimiter is,)splitat 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
- create array like
["" "" "" ""], - 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.LITERALflag - or simply use
split(Pattern.quote("."))and let regex do escaping for you.
I'd fit the answer to OP's level, so I wouldn't recommend split or regexps to him...
If you need substring to second dot, simply find second dot and cut the string to that position...
public class DotSubstring {
public static void main(String[] args) {
String s = "1.2.3.4";
int secondDotPosition = findSecondDotPosition(s);
if (secondDotPosition > 0) {
System.out.println(s.substring(0, secondDotPosition));
} else {
System.out.printf("ERROR: there is not a 2nd dot in '%s'%n", s);
}
}
private static int findSecondDotPosition(String s) {
int result = -1;
int dotsToFind = 2;
char[] ca = s.toCharArray();
for (int i = 0; i < ca.length; ++i) {
if (ca[i] == '.') --dotsToFind;
if (dotsToFind == 0) return i;
}
return result;
}
}
The problem with split for beginner is, that is accepts regexp, that's why it is escaped in Joop Eggen's answe like this str.split("\\.").
And yes, that can be achieved in one line as user3458271 wrote in a comment same as xyz later in answer, just error checking would be more difficult (for example if there are no 2 dots...).
In one line with substring and indexOf:
String output = str.substring(0,str.indexOf(".",str.indexOf(".")+1));
Suppose that you have that string saved in a variable named myString.
String myString = "core/pages/viewemployee.jsff";
String newString = myString.substring(myString.lastIndexOf("/")+1, myString.indexOf("."));
But you need to make the same control before doing substring in this one, because if there aren't those characters you will get a "-1" from lastIndexOf(), or indexOf(), and it will break your substring invocation.
I suggest looking for the Javadoc documentation.
You can solve this with regex (given you only need a group of word characters between the last "/" and "."):
String str="core/pages/viewemployee.jsff";
str=str.replaceFirst(".*/(\\w+).*","$1");
System.out.println(str); //prints viewemployee
java.lang.String.split splits on regular expressions, and . in a regular expression means "any character".
Try temp.split("\\.").
The documentation on split() says:
Splits this string around matches of the given regular expression.
(Emphasis mine.)
A dot is a special character in regular expression syntax. Use Pattern.quote() on the parameter to split() if you want the split to be on a literal string pattern:
String[] words = temp.split(Pattern.quote("."));