Java String Join joinPaths(String... paths)

Here you can find the source of joinPaths(String... paths)

Description

Concatenates multiple paths and separates them with '/'.

Consecutive slashes will be reduced to a single slash in the resulting string.

License

Open Source License

Parameter

Parameter Description
paths the array of paths

Return

the joined path

Declaration

public static String joinPaths(String... paths) 

Method Source Code

//package com.java2s;
/*// w  ww .  j av a  2  s  .c  o m
 * This library is part of Geranium -
 * an open source UI library for GWT.
 *
 * Copyright (c) Alkacon Software GmbH (http://www.alkacon.com)-
 *
 * 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.
 *
 * For further information about Alkacon Software, please see the
 * company website: http://www.alkacon.com
 * 
 * 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
 */

import java.util.Arrays;
import java.util.Iterator;

import java.util.List;

public class Main {
    /**
     * Concatenates multiple paths and separates them with '/'.<p>
     * 
     * Consecutive slashes will be reduced to a single slash in the resulting string.
     * For example, joinPaths("/foo/", "/bar", "baz") will return "/foo/bar/baz".
     * 
     * @param paths the array of paths
     *  
     * @return the joined path 
     */
    public static String joinPaths(String... paths) {

        String result = listAsString(Arrays.asList(paths), "/");
        // result may now contain multiple consecutive slashes, so reduce them to single slashes
        result = result.replaceAll("/+", "/");
        return result;
    }

    /**
     * Returns a string representation for the given list using the given separator.<p>
     * 
     * @param <E> type of list entries
     * @param list the list to write
     * @param separator the item separator string
     * 
     * @return the string representation for the given map
     */
    public static <E> String listAsString(List<E> list, String separator) {

        StringBuffer string = new StringBuffer(128);
        Iterator<E> it = list.iterator();
        while (it.hasNext()) {
            string.append(it.next());
            if (it.hasNext()) {
                string.append(separator);
            }
        }
        return string.toString();
    }
}

Related

  1. joinIterable(Iterable element, String separator)
  2. joinMap(Map map)
  3. joinMap(String keyValueSeperator, String recordSeperator, Map map)
  4. JoinMaps(Map into, Map other)
  5. joinPath(String part1, String part2)
  6. joinRepeat(int size, String s, String delim)
  7. joinSplit(String record, String regex)
  8. joinString(String separator, String... elements)
  9. joinValues(Map map, String separator)