Java File Base Name Get basename(String path, char separator)

Here you can find the source of basename(String path, char separator)

Description

Strips the directory from a file path (if any) using the specified character as a separator.

License

Open Source License

Parameter

Parameter Description
path path
separator separator

Return

path

Declaration

public static String basename(String path, char separator) 

Method Source Code

//package com.java2s;
/* ========================================================================== *
 * Copyright (C) 2004-2005 Pier Fumagalli <http://www.betaversion.org/~pier/> *
 *                            All rights reserved.                            *
 * ========================================================================== *
 *                                                                            *
 * Licensed under the  Apache License, Version 2.0  (the "License").  You may *
 * not use this file except in compliance with the License.  You may obtain a *
 * copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>.       *
 *                                                                            *
 * Unless  required  by applicable  law or  agreed  to  in writing,  software *
 * distributed under the License is distributed on an  "AS IS" BASIS, WITHOUT *
 * WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied.  See the *
 * License for the  specific language  governing permissions  and limitations *
 * under the License.                                                         *
 *                                                                            *
 * ========================================================================== */

public class Main {
    /**//  w  w  w .  j av a  2s.c om
     * Strips the directory from a file path (if any) using the specified
     * character as a separator. If an empty path is supplied then an empty is
     * returned.
     * 
     * @param path path
     * @param separator separator
     * @return path
     */
    public static String basename(String path, char separator) {
        if (path.equals("")) {
            return path;
        }
        while (path.endsWith(String.valueOf(separator))) {
            path = path.substring(0, path.length() - 1);
        }
        int idx = path.lastIndexOf(separator);
        return idx == -1 ? path : path.substring(idx + 1);
    }
}

Related

  1. basename(String path)
  2. basename(String path)
  3. baseName(String path)
  4. basename(String path)
  5. basename(String path)
  6. basename(String s)
  7. basename(String s)
  8. basenamePart(String className)