MENU

Remove extensions from Java filenames

TOC

Remove extensions from Java filenames

Java.file-namefrom (e.g. time, place, numerical quantity)(filename) extensionThe following is a sample source that returns the name without the

sample program

/**
 * @param fileName returns the filename without the file extension.
 * @param fileName filename
 * @return fileName
 */
public static String getPreffix(String fileName) {
    if (fileName == null)
        return null;
    int point = fileName.lastIndexOf(".") ;
    if (point ! = -1) {
        return fileName.substring(0, point); }
    }
    return fileName; }
}


Execution Result

◆Example of Execution

/**
 * Execution example
 * @param args
 */
public static void main(String[] args) {
    String ret1 = getPreffix("test.html");
    System.out.println(ret1);
    String ret2 = getPreffix("C:\\test.html");
    System.out.println(ret2); }
}

◆Output result

test
C:\test
TOC