Problem overview
The method fileExtension receives a file name and returns its extension: the part after the last dot. A name can contain several dots, and only what follows the last one counts. The extension keeps its letters exactly as written. The name has at least one dot, never starts or ends with a dot, and contains no spaces.
For "photo.jpeg" the result is "jpeg". For "archive.tar.gz" it is "gz", not "tar.gz". For "README.MD" it is "MD", with its upper-case letters.
Approach: cut at every dot, keep the last piece
Two ideas combine here. First, split cuts a string into an array of pieces wherever a separator appears, and the separator itself is dropped. Second, the extension is always the last piece, however many dots there are.
For "archive.tar.gz", splitting at the dots gives the array [archive, tar, gz], and the last item is "gz". For "photo.jpeg" it gives two pieces, and the last is "jpeg". The same code handles one dot or ten.
There is one Java-specific catch. The argument of split is a regular expression, a pattern language in which a lone . means "any character". Calling split(".") would treat every character as a separator. On "a.b" that leaves only empty pieces, and Java drops trailing empty strings, so the array has length 0. To cut at a real dot, the pattern must escape it with a backslash: \.. In a Java string literal the backslash itself has to be written twice, so the code reads "\\.".
Step by step
- Split at the dots.
fileName.split("\\.")returns aString[], stored inparts. For"archive.tar.gz"it is[archive, tar, gz]. - Find the last index. Array items are numbered from 0, like the characters of a string, and read with square brackets:
parts[0]is the first. The number of items isparts.length, a field, so there are no parentheses, unlikeString.length(). The last item is atparts.length - 1. - Return that item.
parts[parts.length - 1]is already aString, and it keeps the original case.
The solution in Java
class Solution {
public String fileExtension(String fileName) {
String[] parts = fileName.split("\\.");
return parts[parts.length - 1];
}
}
On the three examples it returns:
photo.jpeg -> jpeg
archive.tar.gz -> gz
README.MD -> MD
Printing the pieces with Arrays.toString shows what split("\\.") does: "a.b.c" becomes [a, b, c] and "archive.tar.gz" becomes [archive, tar, gz].
Edge cases and common mistakes
- Splitting on
".". This is the most frequent error. Because.matches any character,"a.b".split(".")returns an empty array. Thenparts.length - 1is -1 and the program fails with anArrayIndexOutOfBoundsExceptioninstead of returning an extension. - Using a single backslash.
"\."doesn't compile: the backslash starts an escape sequence in the string literal, and\.isn't one. Write"\\."so the regular expression receives\.. - Taking the first piece or the second.
parts[1]works for"photo.jpeg"but returns"tar"for"archive.tar.gz". The last piece is the one that works for every name. - Using
parts.lengthas an index. That index is one past the end and throwsArrayIndexOutOfBoundsException. The last valid index isparts.length - 1. - Changing the case. The extension stays as it is, so
"README.MD"returns"MD", not"md". - A trailing dot. The constraints exclude names that end with a dot. Java's
splitdrops trailing empty strings, so"file.".split("\\.")is just[file], and the method would return"file"instead of an empty extension.
Testing
"photo.jpeg"gives"jpeg"."archive.tar.gz"gives"gz"."README.MD"gives"MD".
