Android File Extension Name Get getFileExtensionFromUrl(String url)

Here you can find the source of getFileExtensionFromUrl(String url)

Description

Returns the file extension or an empty string if there is no extension.

License

Apache License

Parameter

Parameter Description
url a parameter

Return

The file extension of the given url.

Declaration

public static String getFileExtensionFromUrl(String url) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.regex.Pattern;

public class Main {
    /**/*from   w w  w  .  java  2s .  c o m*/
     * Returns the file extension or an empty string if there is no extension.
     * This method is a convenience method for obtaining the extension of a url
     * and has undefined results for other Strings.
     *
     * @param url
     * @return The file extension of the given url.
     */
    public static String getFileExtensionFromUrl(String url) {
        if (url != null && url.length() > 0) {
            int query = url.lastIndexOf('?');
            if (query > 0) {
                url = url.substring(0, query);
            }
            int filenamePos = url.lastIndexOf('/');
            String filename = 0 <= filenamePos ? url
                    .substring(filenamePos + 1) : url;

            // if the filename contains special characters, we don't
            // consider it valid for our matching purposes:
            if (filename.length() > 0
                    && Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)]+",
                            filename)) {
                int dotPos = filename.lastIndexOf('.');
                if (0 <= dotPos) {
                    return filename.substring(dotPos + 1);
                }
            }
        }

        return "";
    }
}

Related

  1. getFileExtension(String fileName)
  2. getFileExtension(String filename)
  3. getFileExtension(String filename, String defExt)
  4. getFileExtensionFromName(String filename)
  5. getFileExtensionFromSource(byte[] picHeader)
  6. getFilenameExtension(String path)
  7. getNameMinusExtension(File file)
  8. getPathExtension(final String path)
  9. getSuffix(String f)