Start learning free

LESSON 73 OF 671 · STRINGS - CHALLENGES

File Extension in Java: split and the Last Array Item

Practice Read the full explanation

Given a file name fileName, return its extension: the part of the name after the last dot.

Split the name at every dot, and the extension is the last of the parts. A name can have more than one dot, like "archive.tar.gz": only what comes after the last dot is the extension. Keep its letters exactly as they are.

 

Example 1:

Input: fileName = "photo.jpeg"
Output: "jpeg"
Explanation: The name has one dot, and "jpeg" comes after it.

Example 2:

Input: fileName = "archive.tar.gz"
Output: "gz"
Explanation: Splitting at the dots gives "archive", "tar" and "gz". The last part, "gz", is the extension.

Example 3:

Input: fileName = "README.MD"
Output: "MD"
Explanation: The extension keeps its upper-case letters.

 

Constraints:

  • fileName has at least one dot, and doesn't start or end with a dot.
  • fileName has no spaces: only English letters, digits, dots, - and _.
STEP 1 OF 2
    Ctrl + Enter to check

    JAVA · STRINGS - CHALLENGES

    File Extension in Java, explained

    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

    1. Split at the dots. fileName.split("\\.") returns a String[], stored in parts. For "archive.tar.gz" it is [archive, tar, gz].
    2. 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 is parts.length, a field, so there are no parentheses, unlike String.length(). The last item is at parts.length - 1.
    3. Return that item. parts[parts.length - 1] is already a String, 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. Then parts.length - 1 is -1 and the program fails with an ArrayIndexOutOfBoundsException instead 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.length as an index. That index is one past the end and throws ArrayIndexOutOfBoundsException. The last valid index is parts.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 split drops 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".
    Keep going with Java650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free