Start learning free

LESSON 74 OF 292 · STRINGS - CHALLENGES

File Extension in Swift: split(separator:) and Arrays

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

    SWIFT · STRINGS - CHALLENGES

    File Extension in Swift, explained

    Problem overview

    The function fileExtension receives a file name and returns its extension: the part after the last dot, with its letters unchanged. A name can have several dots. In "archive.tar.gz" only gz counts as the extension.

    For "photo.jpeg" the answer is "jpeg". For "archive.tar.gz" it is "gz". For "README.MD" it is "MD": the upper-case letters stay. The lesson's names contain at least one dot, never start or end with a dot, and have no spaces.

    Approach: cut at the dots and take the last part

    Every dot is a place to cut the name. Splitting "archive.tar.gz" at the dots gives three parts: archive, tar and gz. Whatever the number of dots, the extension is always the last part, because nothing comes after the last dot. So the plan is to split, then read the last item of the resulting array.

    Step by step

    1. Split the name at the dots with fileName.split(separator: ".") and store the array in a constant named parts, declared with let because it never changes.
    2. Find the last index. Arrays are numbered from 0, so with parts.count items the last one is at parts.count - 1.
    3. Read that item and convert it. The pieces of a split are Substring values, not strings, so wrap the last one in String(...) before returning it.

    The solution in Swift

    class Solution {
        func fileExtension(_ fileName: String) -> String {
            let parts = fileName.split(separator: ".")
            return String(parts[parts.count - 1])
        }
    }
    

    Details worth knowing:

    • The result type of split. For a string, split(separator: ".") returns an array of Substring, written [Substring]. For "archive.tar.gz" it prints as ["archive", "tar", "gz"].
    • The separator is a character. The "." here is a single character, not a pattern. Nothing needs escaping, unlike in a regular expression.
    • Empty parts are dropped by default. split leaves out empty pieces, so "a..b".split(separator: ".") is ["a", "b"]. To keep them, pass omittingEmptySubsequences: false, which gives ["a", "", "b"]. The lesson's names don't have empty parts, so the default is fine.
    • An index out of range crashes. Reading parts[parts.count - 1] from an empty array would stop the program at run time. That can't happen here, since the name has at least one dot and some letters.
    • A shorter way to the last item. The property parts.last returns an optional, so parts.last! or parts.last ?? "" avoid the index arithmetic. The lesson uses the index to practise how arrays are numbered.

    Edge cases and common mistakes

    • Several dots. "archive.tar.gz" gives "gz", not "tar.gz".
    • Dots in the middle of the name. A name like "my-file_1.v2.txt" gives "txt", whatever the earlier parts are.
    • Using the first part. parts[0] is the name without its extension, not the extension.
    • Off-by-one. parts[parts.count] is one past the end and crashes. The last index is parts.count - 1.
    • Changing the case. Keep the letters exactly: "README.MD" gives "MD".
    • Names with no dot or a final dot. The lesson rules these out. For a name with no dot, this code returns the whole name, because the split gives a single part. For "x." it returns "x", since the empty part after the dot is dropped.

    Testing

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