Java Path Concatenate concatPath(String pathName, String name)

Here you can find the source of concatPath(String pathName, String name)

Description

Concatenates specified path with name, and makes sure that only one path-separator is added.

License

Apache License

Declaration

public static String concatPath(String pathName, String name) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from  www. j  ava  2  s  . c  o m
     * Concatenates specified path with name, and makes sure that only one
     * path-separator is added. Examples: "/path1/" + "/name1" -->
     * "/path1/name1", "/path2" + "name2" --> "path1/name1".
     */
    public static String concatPath(String pathName, String name) {
        // param-check
        if ((pathName == null) || (pathName.length() == 0)) {
            return name;
        }
        if ((name == null) || (name.length() == 0)) {
            return pathName;
        }

        StringBuffer result = new StringBuffer(160);
        if (pathName.endsWith("/")) {
            if (name.startsWith("/")) {
                // remove "/"
                result.append(pathName.substring(0, pathName.length() - 1));
            } else {
                result.append(pathName);
            }
        } else {
            result.append(pathName);
            if (!name.startsWith("/")) {
                // add "/"
                result.append("/");
            }
        }
        result.append(name);

        return result.toString();
    }
}

Related

  1. concatPath(String path0, String path1)
  2. concatPath(String path1, String path2)
  3. concatPath(String path1, String path2)
  4. concatPath(String path1, String path2)
  5. concatPath(String root, String fragment)
  6. concatPath(String root, String node)
  7. concatPath(String src, String dst)
  8. concatPath(String... path)