Java String Join joinPath(String part1, String part2)

Here you can find the source of joinPath(String part1, String part2)

Description

join Path

License

Open Source License

Declaration

public static String joinPath(String part1, String part2) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static String joinPath(String part1, String part2) {
        final List<String> parts = new ArrayList<>();
        addPathPart(parts, part1);/* ww  w  .  j a  v  a  2  s.c  o  m*/
        addPathPart(parts, part2);
        return join(parts, "/");
    }

    private static void addPathPart(List<String> parts, String part) {
        if (part != null) {
            final String trimmed = trimSlash(part);
            if (!trimmed.isEmpty()) {
                parts.add(trimmed);
            }
        }
    }

    public static String join(Iterable<? extends Object> values, String delimiter) {
        final StringBuilder sb = new StringBuilder();
        boolean first = true;
        for (Object value : values) {
            if (first) {
                first = false;
            } else {
                sb.append(delimiter);
            }
            sb.append(value);
        }
        return sb.toString();
    }

    private static String trimSlash(String path) {
        path = path.startsWith("/") ? path.substring(1) : path;
        path = path.endsWith("/") ? path.substring(0, path.length() - 1) : path;
        return path;
    }
}

Related

  1. joinedList(T first, T... others)
  2. joinIterable(Iterable element, String separator)
  3. joinMap(Map map)
  4. joinMap(String keyValueSeperator, String recordSeperator, Map map)
  5. JoinMaps(Map into, Map other)
  6. joinPaths(String... paths)
  7. joinRepeat(int size, String s, String delim)
  8. joinSplit(String record, String regex)
  9. joinString(String separator, String... elements)