Java source code to get the extension from a filename

While writing some Java code today, I needed a method to get the extension from a filename. This code solved the problem:

public static String getFilenameExtension(String filename) {
    String extension = "NoExtension";
    int i = filename.lastIndexOf('.');
    if (i > 0) {
        extension = filename.substring(i + 1);
    }
    return extension;
}

Given an input filename like foo.jpg, the getFilenameExtension method returns the “jpg” part as a String.

Warning: I have no idea what this method will do if you pass a null value to it. Don’t do that. (I write my code without null values these days, so I don’t think or account for situations like that.)