Here you can find the source of getLeafName(Path path, boolean includeExtension)
Parameter | Description |
---|---|
path | The path to process. |
includeExtension | Pass in <code>true</code> to leave the extension on the name or <code>false</code> to strip it off. |
public static final String getLeafName(Path path, boolean includeExtension)
//package com.java2s; /*//from w ww . j a va 2 s . c om * Copyright (c) 1998-2017 by Richard A. Wilkes. All rights reserved. * * This Source Code Form is subject to the terms of the Mozilla Public * License, version 2.0. If a copy of the MPL was not distributed with * this file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This Source Code Form is "Incompatible With Secondary Licenses", as * defined by the Mozilla Public License, version 2.0. */ import java.nio.file.Path; public class Main { /** * @param path The path to process. * @return The leaf portion of the path name (everything to the right of the last path * separator). */ public static final String getLeafName(String path) { return getLeafName(path, true); } /** * @param path The path to process. * @param includeExtension Pass in <code>true</code> to leave the extension on the name or * <code>false</code> to strip it off. * @return The leaf portion of the path name (everything to the right of the last path * separator). */ public static final String getLeafName(Path path, boolean includeExtension) { return getLeafName(path.toString(), includeExtension); } /** * @param path The path to process. * @param includeExtension Pass in <code>true</code> to leave the extension on the name or * <code>false</code> to strip it off. * @return The leaf portion of the path name (everything to the right of the last path * separator). */ public static final String getLeafName(String path, boolean includeExtension) { if (path != null) { int index; path = path.replace('\\', '/'); index = path.lastIndexOf('/'); if (index != -1) { if (index == path.length() - 1) { return ""; } path = path.substring(index + 1); } if (!includeExtension) { index = path.lastIndexOf('.'); if (index != -1) { path = path.substring(0, index); } } return path; } return null; } }