Android Utililty Methods File Extension Name Get

List of utility methods to do File Extension Name Get

Description

The list of methods to do File Extension Name Get are organized into topic(s).

Method

StringgetSuffix(String f)
get Suffix
int idx = f.lastIndexOf('.');
if (idx > -1) {
    return f.substring(f.lastIndexOf('.') + 1);
return null;
booleanhasExtension(String filename, String extension)
Returns whether or not the provided filename has the provided extension.
if (Util.isBlank(extension)) {
    return false;
String fileExtension = getExtension(filename);
if (Util.isBlank(fileExtension)) {
    return false;
return fileExtension.equalsIgnoreCase(extension.replace(".", ""));
...
intindexOfExtension(String filename)
index Of Extension
if (filename == null) {
    return -1;
int extensionPos = filename.lastIndexOf(".");
int lastSeparator = filename.lastIndexOf("/");
return (lastSeparator > extensionPos ? -1 : extensionPos);
booleanisValidFileExtension(String extension)
Checks if the extension is valid.
for (int i = 0; i < extension.length(); i++) {
    char c = extension.charAt(i);
    if (!(Character.isDigit(c) || Character.isLetter(c) || c == '_')) {
        return false;
return true;
StringparseFileExtension(String filename)
Parse the filename and return the file extension.
if (filename == null) {
    return null;
int pos = filename.lastIndexOf('.');
if (pos < 0 || (pos + 1) >= filename.length()) {
    return null;
return filename.substring(pos + 1);
...
StringparseFileExtension(String filename)
Returns the extension of the given filename.
String ext = null;
if (filename != null) {
    int dot = filename.lastIndexOf('.');
    if (dot > 0 && dot < filename.length()) {
        ext = filename.substring(dot + 1);
return ext;
...
StringremoveExtension(String file)
remove Extension
int extPos = file.lastIndexOf('.');
if (extPos != -1 && extPos > file.indexOf(File.separatorChar)) {
    file = file.substring(0, extPos);
return file;
StringremoveExtension(String s)
Removes the path and extension from the input
String separator = System.getProperty("file.separator");
String filename;
int lastSeparatorIndex = s.lastIndexOf(separator);
if (lastSeparatorIndex == -1) {
    filename = s;
} else {
    filename = s.substring(lastSeparatorIndex + 1);
int extensionIndex = filename.lastIndexOf(".");
if (extensionIndex == -1) {
    return filename;
return filename.substring(0, extensionIndex);
StringgetExtension(final String fileName)
get Extension
return fileName.substring(fileName.lastIndexOf(".") + 1)
        .toLowerCase();
StringgetFileExtenSion(File file)
get File Exten Sion
String ex;
String filename;
try {
    filename = file.getName();
    ex = filename.substring(filename.lastIndexOf(".") + 1);
} catch (Exception e) {
    return "";
return ex.toLowerCase(Locale.getDefault());