Start learning free

LESSON 73 OF 289 · STRINGS - CHALLENGES

File Extension in C#: Split at the Dots

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

    C# · STRINGS - CHALLENGES

    File Extension in C#, explained

    Problem overview

    The method FileExtension receives a file name and returns its extension: the part after the last dot. "photo.jpeg" gives "jpeg". "archive.tar.gz" has two dots, and only what follows the last one counts, so it gives "gz". "README.MD" gives "MD": the extension keeps its letters exactly as they are, upper case included.

    Approach: cut the name into parts

    If you cut the name at every dot, the extension is the last part, however many dots there are. "archive.tar.gz" is cut into "archive", "tar" and "gz", and the answer is the last one. In C#, Split('.') does the cutting and returns a string[], an array of strings.

    string[] parts = "archive.tar.gz".Split('.');
    Console.WriteLine(parts.Length);
    Console.WriteLine(string.Join(",", parts));
    3
    archive,tar,gz

    The argument is a char in single quotes, because the separator here is one character. The dots themselves are not part of any piece.

    Step by step

    1. Split the name. Store the result of fileName.Split('.') in a string[] named parts. For "photo.jpeg" it holds "photo" and "jpeg".
    2. Take the last item. Arrays are numbered from 0, like strings, and have a Length. The last item is at index parts.Length - 1. For two parts that is index 1, and for three parts it is index 2.

    Using parts.Length - 1, instead of a fixed number, is what makes the same code work for names with one dot and for names with several.

    The solution in C#

    class Solution {
        public string FileExtension(string fileName) {
            string[] parts = fileName.Split('.');
            return parts[parts.Length - 1];
        }
    }

    The method returns the part exactly as it was in the name, so no case conversion happens. The original fileName is not changed: strings are immutable, and Split creates new strings for the parts.

    Other ways to find the extension

    You will see two more approaches. The first finds the position of the last dot with LastIndexOf and takes the rest of the string. The second uses the library method Path.GetExtension (in System.IO), but note that it keeps the dot in its result:

    string f = "archive.tar.gz";
    Console.WriteLine(f.Substring(f.LastIndexOf('.') + 1));
    Console.WriteLine(Path.GetExtension(f));
    gz
    .gz

    The lesson asks for the extension without the dot, so the split version, or the LastIndexOf one, is the match.

    Edge cases and common mistakes

    • Using index 1. parts[1] works for "photo.jpeg" but returns "tar" for "archive.tar.gz". The last index depends on how many parts there are.
    • Using parts.Length as the index. The last valid index is parts.Length - 1; one past it throws an IndexOutOfRangeException.
    • Double quotes in Split. A single character separator is a char, '.'. A name without any dot would give an array with one item, the whole name, but the lesson promises at least one dot.
    • A trailing dot. For a name like "a.b.", splitting gives an empty last part, and the method would return an empty string. The lesson excludes names that end with a dot.

    Because the lesson says the name has at least one dot, Split always returns at least two parts, so the last item is never the whole name by accident. Without that promise, a name such as "noext" would give an array with a single item, and the method would return the whole name as if it were an extension. A method for arbitrary file names would first check for a dot, for example with Contains('.'). Here the lesson's input rules make that check unnecessary.

    Testing

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