Start learning free

LESSON 80 OF 289 · STRINGS - CHALLENGES

File Extension in TypeScript: split() and the Last 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

    TYPESCRIPT · STRINGS - CHALLENGES

    File Extension in TypeScript, explained

    Problem overview

    The method fileExtension receives a file name, fileName, and returns its extension: the part after the last dot. For "photo.jpeg" that is "jpeg". A name can contain several dots, and only the last one matters: "archive.tar.gz" gives "gz". The extension keeps its letters exactly as written, so "README.MD" gives "MD". The lesson guarantees that the name has at least one dot and doesn't start or end with one.

    Approach

    The idea is to cut the name at every dot and keep the last piece. The split method of a string does the cutting: fileName.split(".") returns an array of strings, the parts between the separators, and the separator itself is not included. For "archive.tar.gz" the result is ["archive", "tar", "gz"]. For "photo.jpeg" it is ["photo", "jpeg"].

    Whatever the number of dots, the extension is always the last item of that array.

    Step by step

    1. Split the name at the dots. Store the array in parts, declared with let and the type string[], an array of strings.
    2. Read the last item. An array is numbered from 0, like a string, and has a length. Its last item is at index parts.length - 1. Return it.

    The solution in TypeScript

    class Solution {
        fileExtension(fileName: string): string {
            let parts: string[] = fileName.split(".");
            return parts[parts.length - 1];
        }
    }
    

    A few details about the code:

    • The separator is a string, not a pattern. split(".") cuts at the dot character itself. The dot has no special meaning here, because the argument is a plain string and not a regular expression.
    • string[] is an array type. It says that every item of parts is a string. The annotation is checked by the compiler and erased from the JavaScript that runs.
    • The array's length is one more than the number of dots. "archive.tar.gz" has two dots and gives 3 parts. The last index is therefore parts.length - 1, which is 2 here.

    The string[] that split returns is a new array. The original fileName is not changed, because strings can't be modified in place.

    Tracing the example

    For "archive.tar.gz", parts is ["archive", "tar", "gz"] and parts.length is 3. The index 3 - 1 is 2, and parts[2] is "gz". For "my-file_v2.final.txt" the same code returns "txt": the hyphen and the underscore are ordinary characters and don't affect the cut.

    Other ways to write it

    The method at reads from the end, so fileName.split(".").at(-1) also gives "gz". Another approach finds the last dot with lastIndexOf("."), which is 11 in "archive.tar.gz", and slices from the next position: fileName.slice(fileName.lastIndexOf(".") + 1). Both return "gz". The lesson's version teaches arrays and indexing, and it needs no arithmetic with positions in the string.

    Edge cases and common mistakes

    • Taking parts[1]. That works for "photo.jpeg" but returns "tar" for "archive.tar.gz". The extension is the last part, not the second.
    • Using parts[parts.length]. That index is one past the end and gives undefined.
    • Changing the case. The extension is returned as written, so "MD" stays "MD".
    • A name without a dot. The lesson rules it out. For "README", split(".") gives ["README"], so the code would return the whole name, "README".

    Testing

    • "photo.jpeg" gives "jpeg".
    • "archive.tar.gz" gives "gz".
    • "README.MD" gives "MD".
    • "my-file_v2.final.txt" gives "txt".
    Keep going with TypeScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free