Java File Extension Name Extract extractFileExtension(String path)

Here you can find the source of extractFileExtension(String path)

Description

Extract the file extension from the given URI path.

License

Apache License

Parameter

Parameter Description
path the URI path (e.g. "/products/index.html")

Return

the extracted file extension (e.g. "html")

Declaration

public static String extractFileExtension(String path) 

Method Source Code

//package com.java2s;
/*/*from   w ww  .j ava  2  s .  com*/
 * Copyright 2002-2016 the original author or authors.
 *
 * 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 file extension from the given URI path.
     * @param path the URI path (e.g. "/products/index.html")
     * @return the extracted file extension (e.g. "html")
     * @since 4.3.2
     */
    public static String extractFileExtension(String path) {
        int end = path.indexOf('?');
        if (end == -1) {
            end = path.indexOf('#');
            if (end == -1) {
                end = path.length();
            }
        }
        int begin = path.lastIndexOf('/', end) + 1;
        int paramIndex = path.indexOf(';', begin);
        end = (paramIndex != -1 && paramIndex < end ? paramIndex : end);
        int extIndex = path.lastIndexOf('.', end);
        if (extIndex != -1 && extIndex > begin) {
            return path.substring(extIndex + 1, end);
        }
        return null;
    }
}

Related

  1. extractFileExtension(String fileName)
  2. extractFileExtension(String filename)
  3. extractFileExtension(String filename)
  4. extractFileExtension(String fileName)
  5. extractFileExtension(String filePath)
  6. extractFileExtensionFromPath(String path)
  7. getFileExtension(File f)
  8. getFileExtension(File file)
  9. getFileExtension(File file)