Java List Join join(List paths)

Here you can find the source of join(List paths)

Description

join

License

Open Source License

Declaration

public static String join(List<String> paths) 

Method Source Code

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

import java.util.List;

public class Main {
    public static String join(List<String> paths) {
        if (paths == null || paths.isEmpty()) {
            return null;
        }/*  w  ww  .  j ava2  s  .c o  m*/
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paths.size(); i++) {
            if (i == 0) {
                sb.append(trimTrailingSlash(paths.get(i)));
            } else {
                sb.append("/").append(trimSlash(paths.get(i)));
            }
        }
        return sb.toString();
    }

    public static String join(String... paths) {
        if (paths == null || paths.length == 0) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paths.length; i++) {
            if (i == 0) {
                sb.append(trimTrailingSlash(paths[i]));
            } else {
                sb.append("/");
                sb.append(trimSlash(paths[i]));
            }
        }
        return sb.toString();
    }

    public static String join(String[] paths, int offset, int length) {
        if (paths == null || paths.length == 0 || offset >= paths.length) {
            return null;
        }
        int end = offset + length;
        if (end > paths.length) {
            end = paths.length;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = offset; i < end; i++) {
            if (i == 0) {
                sb.append(trimTrailingSlash(paths[i]));
            } else {
                sb.append("/").append(trimSlash(paths[i]));
            }
        }
        return sb.toString();
    }

    public static String trimTrailingSlash(String path) {

        if (path == null || path.isEmpty()) {
            return path;
        }
        while (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }

    public static String trimSlash(String path) {
        return trimTrailingSlash(trimLeadingSlash(path));
    }

    public static String trimLeadingSlash(String path) {
        if (path == null || path.isEmpty()) {
            return path;
        }
        while (path.startsWith("/")) {
            path = path.substring(1);
        }
        return path;
    }
}

Related

  1. join(List lst, int start, int end)
  2. join(List lst, String separator)
  3. join(List members, boolean quote)
  4. join(List p_sStrList, String p_sDelimiter)
  5. join(List parts, String separator)
  6. join(List s, String delim)
  7. join(List s, String sep)
  8. join(List strings)
  9. join(List strings)