return the file extension of a file using strings only

C Sharp - C# (programming language)
Post Reply
dzcadii
Posts: 94
Joined: Sun Mar 01, 2015 4:36 am

return the file extension of a file using strings only

Post by dzcadii » Sun Mar 01, 2015 10:47 pm

If you find yourself in a situation where you have to use string(s) and cannot use the Path class:

Code: Select all

public static string GetFileExtension(string fileName)
{
    string ext = string.Empty;
    int fileExtPos = fileName.LastIndexOf(".", StringComparison.Ordinal);
    if (fileExtPos >= 0)
        ext = fileName.Substring(fileExtPos, fileName.Length - fileExtPos);

    return ext;
}

Post Reply