集册 Java实例教程 将提供路径中的最终组件替换为指定的新组件。

将提供路径中的最终组件替换为指定的新组件。

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

559
用指定的新组件替换提供的路径中的最终组件。

// Copyright (C) 2001-2012 Michael Bayne, et al.

//package com.nowjava;
/**来自 n o w j a v a . c o m**/

public class Main {

    /**

     * Replaces the final component in the supplied path with the

     * specified new component. For example, if <code>/foo/bar/baz</code>

     * was provided as the source path, <code>baz</code> would be replaced

     * with the supplied new path component. If no slashes occur in the

     * path, the entire path will be replaced. Note that this function is

     * intended for use on URLs rather than filesystem paths and thus

     * always uses forward slash rather than the platform defined path

     * separator.

     */

    public static String replaceFinalComponent(String source,

            String newComponent) {

        int sidx = source.lastIndexOf("/");

        if (sidx != -1) {

     
展开阅读全文