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
- Split the name. Store the result of
fileName.Split('.')in astring[]namedparts. For"photo.jpeg"it holds"photo"and"jpeg". - Take the last item. Arrays are numbered from 0, like strings, and have a
Length. The last item is at indexparts.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.Lengthas the index. The last valid index isparts.Length - 1; one past it throws anIndexOutOfRangeException. - Double quotes in
Split. A single character separator is achar,'.'. 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
| fileName | Result |
|---|---|
"photo.jpeg" | "jpeg" |
"archive.tar.gz" | "gz" |
"README.MD" | "MD" |
