/** * Removes the extension from a filename. * <p> * This method returns the textual part of the filename before the last dot. * There must be no directory separator after the dot. * * <pre> * foo.txt --> foo * a\b\c.jpg --> a\b\c * a\b\c --> a\b\c * a.b\c --> a.b\c * </pre> * <p> * * @param filename the filename to query, null returns null * @return the filename minus the extension */ public static String removeExtension(String filename) { if (filename == null) { return null; } int dot = getExtensionIndex(filename); if (dot == -1) { return filename; } else { return filename.substring(0, dot); } }
/** * Gets the extension of a filename. * <p> * This method returns the textual part of the filename after the last dot. * There must be no directory separator after the dot. * * <pre> * a/b/c.txt --> txt * a.b.txt --> txt * a/b.txt/c --> "" * a/b/c --> "" * </pre> * <p> * * @param filename the filename to retrieve the extension of. * @return the extension of the file or an empty string if none exists or * {@code null} if the filename is {@code null}. */ public static String getExtension(String filename) { if (filename == null) { return null; } int dot = getExtensionIndex(filename); if (dot == -1) { return StringUtils.EMPTY; } else { return filename.substring(dot + 1); } }
/** * Removes the extension from a filename. * <p> * This method returns the textual part of the filename before the last dot. * There must be no directory separator after the dot. * * <pre> * foo.txt --> foo * a\b\c.jpg --> a\b\c * a\b\c --> a\b\c * a.b\c --> a.b\c * </pre> * <p> * * @param filename the filename to query, null returns null * @return the filename minus the extension */ public static String removeExtension(String filename) { if (filename == null) { return null; } int dot = getExtensionIndex(filename); if (dot == -1) { return filename; } else { return filename.substring(0, dot); } }
/** * Gets the extension of a filename. * <p> * This method returns the textual part of the filename after the last dot. * There must be no directory separator after the dot. * * <pre> * a/b/c.txt --> txt * a.b.txt --> txt * a/b.txt/c --> "" * a/b/c --> "" * </pre> * <p> * * @param filename the filename to retrieve the extension of. * @return the extension of the file or an empty string if none exists or * {@code null} if the filename is {@code null}. */ public static String getExtension(String filename) { if (filename == null) { return null; } int dot = getExtensionIndex(filename); if (dot == -1) { return StringUtils.EMPTY; } else { return filename.substring(dot + 1); } }