Here you can find the source of extractExtension(String url)
Parameter | Description |
---|---|
url | the url from which the extension needs to be extracted. |
Parameter | Description |
---|---|
NullPointerException | if the URL presented is <code>null</code> |
public static String extractExtension(String url)
//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; } }