Here you can find the source of extractFileExtension(String path)
Parameter | Description |
---|---|
path | the URI path (e.g. "/products/index.html") |
public static String extractFileExtension(String path)
//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; } }