Java Path Concatenate concatPaths(String path1, String path2)

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

Description

Concatenate two paths together, inserting a '/' if needed, and ensuring that there is no '//' at the join.

License

Open Source License

Parameter

Parameter Description
path1 first path
path2 secpnd path

Return

concatenated paths

Declaration

public static String concatPaths(String path1, String path2) 

Method Source Code

//package com.java2s;
/** (C) Copyright 2006 Hewlett-Packard Development Company, LP
    // ww w .  j  a v a 2  s  .  c  o  m
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
    
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
    
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
 For more information: www.smartfrog.org
    
 */

public class Main {
    /**
     * Concatenate two paths together, inserting a '/' if needed, and ensuring
     * that there is no '//' at the join.
     *
     * @param path1 first path
     * @param path2 secpnd path
     * @return concatenated paths
     */
    public static String concatPaths(String path1, String path2) {
        StringBuilder buffer = new StringBuilder(path1.length() + path2.length() + 1);
        boolean endsWithSlash = path1.endsWith("/");
        boolean beginsWithSlash = path2.startsWith("/");
        buffer.append(path1);
        if (!endsWithSlash) {
            buffer.append('/');
        }
        if (beginsWithSlash) {
            buffer.append(path2.substring(1));
        } else {
            buffer.append(path2);
        }
        return buffer.toString();
    }
}

Related

  1. concatPath(String root, String fragment)
  2. concatPath(String root, String node)
  3. concatPath(String src, String dst)
  4. concatPath(String... path)
  5. concatPaths(String path1, String path2)
  6. concatPaths(String prefix, String postfix)
  7. concatPathsStrings(String head, String tail)