Java tutorial
//package com.java2s; public class Main { /** * <p>Determines the parent directory of the given path. This is similar to * dirname(1). This assumes that path components are separated by forward * slashes.</p> * * <p>The returned path omits the last slash and trailing component; * for example, "/foo/bar.txt" becomes "/foo". There are a special cases: * <ul> * <li> If the last slash is the first character in the input, * the return value is "/". * <li> If there are no slashes in the input, "." is returned. * </ul> * </p> * * @param path the path to strip * @return the parent path, as described above */ public static String dirname(String path) { int lastSlash = path.lastIndexOf("/"); if ("/".equals(path) || lastSlash == 0) { return "/"; } else if (lastSlash == -1) { return "."; } else { return path.substring(0, lastSlash); } } }