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
- Split the name at the dots with
fileName.split(separator: ".")and store the array in a constant namedparts, declared withletbecause it never changes. - Find the last index. Arrays are numbered from 0, so with
parts.countitems the last one is atparts.count - 1. - Read that item and convert it. The pieces of a split are
Substringvalues, not strings, so wrap the last one inString(...)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 ofSubstring, 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.
splitleaves out empty pieces, so"a..b".split(separator: ".")is["a", "b"]. To keep them, passomittingEmptySubsequences: 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.lastreturns an optional, soparts.last!orparts.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 isparts.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".
