Here you can find the source of getFileExtension(String filename, String extensionSeparator)
Parameter | Description |
---|---|
filename | the file name. |
extensionSeparator | the text or characters that separates file name from extension. |
public static String getFileExtension(String filename, String extensionSeparator)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009-2016 Black Rook Software * All rights reserved. This program and the accompanying materials * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html ******************************************************************************/ import java.io.*; public class Main { /**/*from w w w.ja va2s .c o m*/ * Returns the extension of a filename. * @param filename the file name. * @param extensionSeparator the text or characters that separates file name from extension. * @return the file's extension, or an empty string for no extension. * @since 2.5.0 */ public static String getFileExtension(String filename, String extensionSeparator) { int extindex = filename.lastIndexOf(extensionSeparator); if (extindex >= 0) return filename.substring(extindex + 1); return ""; } /** * Returns the extension of a file's name. * @param file the file. * @param extensionSeparator the text or characters that separates file name from extension. * @return the file's extension, or an empty string for no extension. * @since 2.5.0 */ public static String getFileExtension(File file, String extensionSeparator) { return getFileExtension(file.getName(), extensionSeparator); } /** * Returns the extension of a filename. * Assumes the separator to be ".". * @param filename the file name. * @return the file's extension, or an empty string for no extension. * @since 2.5.0 */ public static String getFileExtension(String filename) { return getFileExtension(filename, "."); } /** * Returns the extension of a file's name. * Assumes the separator to be ".". * @param file the file. * @return the file's extension, or an empty string for no extension. * @since 2.5.0 */ public static String getFileExtension(File file) { return getFileExtension(file.getName(), "."); } }