Start learning free

LESSON 74 OF 291 · STRINGS - CHALLENGES

File Extension in Scala: split and the Last Part

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

    SCALA · STRINGS - CHALLENGES

    File Extension in Scala, explained

    Problem overview

    The method fileExtension receives a file name and returns its extension: the part after the last dot. "photo.jpeg" gives "jpeg". A name can hold several dots, and only the last one counts: "archive.tar.gz" gives "gz". The extension keeps its letters exactly as written, so "README.MD" gives "MD". The lesson guarantees at least one dot, and that the name neither starts nor ends with a dot.

    Approach

    Cut the name at every dot, and the extension is the last of the pieces. This works whether the name has one dot or five, because the last piece is always whatever follows the last dot. It means the method needs two things: a way to split a string, and a way to read the last item of the result.

    The split has one trap. String.split doesn't take a plain separator: it takes a regular expression, and in a regular expression a lone . means "any character". Splitting "archive.tar.gz" with split(".") treats every character as a separator, and the result is an empty array, with length 0. To cut at real dots, the dot has to be escaped with a backslash, and the backslash itself has to be doubled inside a Scala string literal: "\\.".

    Step by step

    1. Split the name at the dots with fileName.split("\\.") and keep the result in a val named parts. For "archive.tar.gz" it is the array [archive, tar, gz].
    2. Read the last item. Scala reads an array item with parentheses, parts(i), and the indexes start at 0. An array of parts.length items ends at index parts.length - 1, so that is where the extension is. Return it.

    The solution in Scala

    class Solution {
        def fileExtension(fileName: String): String = {
            val parts = fileName.split("\\.")
            return parts(parts.length - 1)
        }
    }
    

    A few Scala details:

    • split returns an Array[String]. Arrays are read with parts(i), not with square brackets, which Scala uses for type parameters.
    • "\\." is two characters, a backslash and a dot, once the string literal is read. The regular expression engine then sees \., a literal dot.
    • val makes parts a name that can't be reassigned. The array's items can still be changed, but this method never does.
    • The standard library also has parts.last, which gives the same item. The lesson indexes by hand to practise the arithmetic.

    Tracing an example

    For "photo.jpeg", split("\\.") gives two parts, photo and jpeg, so the last index is 1 and the method returns "jpeg". For "archive.tar.gz", the parts are archive, tar and gz, the last index is 2, and the result is "gz".

    Edge cases and common mistakes

    • Splitting with ".". The unescaped dot matches every character, so "a.b".split(".") is an empty array, and reading parts(parts.length - 1) from it throws an exception.
    • Reading index parts.length. The last valid index is one less than the length. Index parts.length is out of range.
    • Taking the first part. For "archive.tar.gz", parts(0) is "archive", the name without its extension.
    • Trailing dots. Scala's split drops empty pieces at the end, so "a.b.".split("\\.") is [a, b] and the method would return "b". The lesson's constraints exclude names that end with a dot.

    Testing

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