Java File Extension Name Get extractExtension(String url)

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

Description

Extract the extension from the given URL.

License

Apache License

Parameter

Parameter Description
url the url from which the extension needs to be extracted.

Exception

Parameter Description
NullPointerException if the URL presented is <code>null</code>

Return

the extracted extension

Declaration

public static String extractExtension(String url) 

Method Source Code

//package com.java2s;
/**//  w  ww. java2s  .c o  m
 *
 * jerry - Common Java Functionality
 * Copyright (c) 2012-2015, Sandeep Gupta
 * 
 * http://sangupta.com/projects/jerry
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

public class Main {
    /**
     * Extract the extension from the given URL.
     * 
     * @param url
     *            the url from which the extension needs to be extracted.
     * 
     * @return the extracted extension
     * 
     * @throws NullPointerException
     *             if the URL presented is <code>null</code>
     */
    public static String extractExtension(String url) {
        // check for any slash characters that remain
        int index = url.lastIndexOf('/');
        if (index != -1) {
            url = url.substring(index + 1);
        }

        // now for the dot part
        index = url.lastIndexOf('.');

        // check if extension present
        if (index == -1) {
            return null;
        }

        url = url.substring(index + 1);

        // query param
        int end = url.indexOf('?');
        if (end != -1) {
            url = url.substring(0, end);
        }

        // anchor name
        end = url.indexOf('#');
        if (end != -1) {
            url = url.substring(0, end);
        }

        return url;
    }
}

Related

  1. extractExtension(final String fileName)
  2. extractExtension(String fileName)
  3. extractExtension(String name)
  4. extractExtension(String s)
  5. extractExtension(String strFilename)
  6. extractExtensions(String fileName)
  7. fileExt(String name)
  8. fileExt(String url)
  9. fileExtension(CharSequence path)