路径名

欢马劈雪     最近更新时间:2020-01-02 10:19:05

527
路径名


//package com.nowjava;

/*
时 代 J     a    v  a - nowjava.com
*/

public class Main {

    public static final String SEPARATOR = "/";


    public static String pathName(String path) {

        String[] split = cleanPath(path).split(SEPARATOR);

        if (split.length > 0) {

            return split[split.length - 1];

        }

        return null;

    }


    /**

     * Clean the path string by removing leading and trailing slashes and removing duplicate slashes.

     * @param path input path

     * @return cleaned path string

     */

    public static String cleanPath(String path) {

        if (path.endsWith(SEPARATOR)) {

            path = path.replaceAll(SEPARATOR + "+$", "");

        }

        if (path.startsWith(SEPARATOR)) {

            path = path.replaceAll("^" + SEPARATOR + 
展开阅读全文