Java File Base Name Get baseName(String filename)

Here you can find the source of baseName(String filename)

Description

Returns the basename of a filename.

License

Open Source License

Declaration

public static String baseName(String filename) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w  w  w. ja  va  2s  .  c o m*/
     * Returns the basename of a filename.  Does this by returning only the
     * portion of the string after the last slash or backslash character 
     * (either one if both present).
     * 
     * If null is given, or if the name ends in a slash or backslash, an empty 
     * string is returned.  Whitespace is trimmed from the returned name.
     */
    public static String baseName(String filename) {
        if (filename == null)
            return "";

        String basename = filename;
        int idx = basename.lastIndexOf('/');
        if (idx >= 0) {
            idx++;
            if (basename.length() == idx)
                return "";
            else
                basename = basename.substring(idx);
        }
        idx = basename.lastIndexOf('\\');
        if (idx >= 0) {
            idx++;
            if (basename.length() == idx)
                return "";
            else
                basename = basename.substring(idx);
        }
        return basename.trim();
    }
}

Related

  1. basename(final String name)
  2. baseName(String controllerName)
  3. baseName(String filename)
  4. baseName(String filename)
  5. basename(String filename)
  6. baseName(String fn)
  7. baseName(String name)
  8. basename(String name, String split)
  9. basename(String path)