Start learning free

LESSON 74 OF 291 · STRINGS - CHALLENGES

File Extension in Kotlin: Splitting a String

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

    KOTLIN · STRINGS - CHALLENGES

    File Extension in Kotlin, explained

    Problem overview

    The function fileExtension receives a file name and returns its extension: the part after the last dot. A name can contain several dots, and only the text after the final one counts. The extension keeps its letters exactly as they are.

    For "photo.jpeg" the answer is "jpeg". For "archive.tar.gz" it is "gz". For "README.MD" it is "MD", with the capital letters kept. The lesson guarantees at least one dot, and no dot at the start or the end of the name.

    Approach

    Cut the name at every dot and take the last piece. split(".") turns a string into a list of strings, one for each part between the separators. Whatever the number of dots, the extension is the last item of that list.

    Step by step

    1. Split the name with fileName.split(".") and keep the list in a val named parts. Every dot is a cut, so "archive.tar.gz" gives three parts.
    2. Read the last item. A list is indexed like a string, with square brackets, and its length is parts.size. The last index is parts.size - 1.

    The solution in Kotlin

    class Solution {
        fun fileExtension(fileName: String): String {
            val parts = fileName.split(".")
            return parts[parts.size - 1]
        }
    }
    

    Details worth knowing:

    • The result is a List<String>. Printing "archive.tar.gz".split(".") shows [archive, tar, gz]. The list is read-only, which is all this function needs.
    • size, not length. Strings have length; lists have size. Mixing them up is a compile error.
    • The dot is plain text here. In Kotlin, split(".") with a string argument splits on that exact text. It isn't a pattern where a dot means "any character", so no escaping is needed. (Only the Regex overload treats it as a pattern.)
    • Shortcuts. fileName.substringAfterLast(".") returns the text after the last dot in one call, and parts.last() returns the last item of a list. For "archive.tar.gz" both give "gz".

    Tracing the examples

    "photo.jpeg".split(".") gives two parts, photo and jpeg, so parts.size is 2 and the last index is 1: the answer is "jpeg".

    "archive.tar.gz" gives [archive, tar, gz]. The size is 3, the last index is 2, and the answer is "gz". The name has two dots, but the code doesn't care how many there are.

    "README.MD" gives [README, MD], and the answer is "MD". split copies the text as it is, so no case changes.

    Edge cases and common mistakes

    • Taking parts[1]. That works for "photo.jpeg" but returns "tar" for "archive.tar.gz". The last item is the one to read.
    • Using parts.size as the index. The valid indexes end at parts.size - 1. Index parts.size throws an IndexOutOfBoundsException (an ArrayIndexOutOfBoundsException, a kind of it, on arrays).
    • Names without a dot. The lesson rules them out. For "noDot" the split gives a list with one item, [noDot], so the function would return the whole name.
    • Empty parts. Two dots in a row, as in "a..b", produce an empty string between them: [a, , b]. A dot at the end would end the list with an empty string. The lesson excludes names that end with a dot, which keeps the last part non-empty.

    Testing

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