Java File Extension Name Extract getFileExtension(String filename, String extensionSeparator)

Here you can find the source of getFileExtension(String filename, String extensionSeparator)

Description

Returns the extension of a filename.

License

Open Source License

Parameter

Parameter Description
filename the file name.
extensionSeparator the text or characters that separates file name from extension.

Return

the file's extension, or an empty string for no extension.

Declaration

public static String getFileExtension(String filename, String extensionSeparator) 

Method Source Code

//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(), ".");
    }
}

Related

  1. getFileExtension(File path)
  2. getFileExtension(final String fileName)
  3. getFileExtension(final String filenamePath)
  4. getFileExtension(String fileName)
  5. getFileExtension(String filename)
  6. getFileExtension(String filePath)
  7. getFileExtensionIgnoringGz(File f)