Java Path Merge mergePath(String path1, String path2)

Here you can find the source of mergePath(String path1, String path2)

Description

Merge two path to one path

License

Open Source License

Parameter

Parameter Description
path1 in the head
path2 in the back

Return

full path

Declaration

public static String mergePath(String path1, String path2) 

Method Source Code

//package com.java2s;

public class Main {
    /**//w ww.j  av  a  2 s  .c  o m
     * Merge two path to one path
     * 
     * @param path1 in the head
     * @param path2 in the back
     * @return full path
     */
    public static String mergePath(String path1, String path2) {
        String hp = path1 == null ? "" : path1;
        String hb = path2 == null ? "" : path2;

        String splider1 = "";
        if (hp.endsWith("/") || hp.endsWith("\\")) {
            splider1 = hp.substring(hp.length() - 1, hp.length());
            hp = hp.substring(0, hp.length() - 1);
        }
        if (hb.startsWith("/") || hb.startsWith("\\")) {
            splider1 = hb.substring(0, 1);
            hb = hb.substring(1, hb.length());
        }
        if (splider1.length() == 0) {
            splider1 = "/";
        }
        return hp + splider1 + hb;
    }
}

Related

  1. merge(String path, String relativePath)
  2. merge(String path, String relativePath, boolean hasAuthority)
  3. mergePath(String... paths)
  4. mergePaths(String part1, String part2)