Java String Join join(String... paths)

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

Description

join

License

Open Source License

Declaration

public static final String join(String... paths) 

Method Source Code

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

import java.util.List;

public class Main {
    public final static char SLASH_CHAR = '/';
    public final static char BACK_SLASH_CHAR = '\\';

    public static final String join(char separator, String... paths) {
        if (paths == null || paths.length == 0) {
            return null;
        }//from   w ww .j a  v a  2 s . c o m
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < paths.length; i++) {
            if (i == 0) {
                sb.append(trimTrailingSeparator(paths[i], separator));
            } else {
                sb.append(separator).append(trimSeparator(paths[i], separator));
            }
        }
        return sb.toString();
    }

    public static final String join(String... paths) {
        return join(SLASH_CHAR, paths);
    }

    public static final String join(char separator, List<String> paths) {
        if (paths == null || paths.isEmpty()) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        int size = paths.size();
        for (int i = 0; i < size; i++) {
            if (i == 0) {
                sb.append(trimTrailingSeparator(paths.get(i), separator));
            } else {
                sb.append(separator).append(trimSeparator(paths.get(i), separator));
            }
        }
        return sb.toString();
    }

    public static final String join(List<String> paths) {
        return join(SLASH_CHAR, paths);
    }

    private static String join(String[] components, char separator, int offset, int length) {
        if (components == null || components.length == 0 || offset >= components.length) {
            return null;
        }
        int end = offset + length;
        if (end > components.length) {
            end = components.length;
        }
        StringBuilder sb = new StringBuilder();
        for (int i = offset; i < end; i++) {
            if (i == 0) {
                sb.append(components[i]);
            } else {
                sb.append(separator).append(components[i]);
            }
        }
        return sb.toString();
    }

    public static final String trimTrailingSeparator(String path, char separator) {
        if (path != null) {
            String p = normalise(path, separator);
            if (p.length() > 1) {
                return p.replaceAll("\\" + separator + "+$", "");
            } else {
                return p;
            }
        }
        return null;
    }

    public static final String trimSeparator(String path, char separator) {
        return trimLeadingSeparator(trimTrailingSeparator(path, separator), separator);
    }

    public static final String normalise(String path) {
        return normalise(path, BACK_SLASH_CHAR, SLASH_CHAR);
    }

    public static final String normalise(String path, char separator) {
        return normalise(path, separator, separator);
    }

    public static final String normalise(String path, char separator, char newSeparator) {
        if (path != null) {
            String p = path.trim();
            if (separator != newSeparator) {
                p = p.replace(separator, newSeparator);
            }
            p.replaceAll("\\" + newSeparator + "{2,}", "\\" + newSeparator);
            return p;
        }
        return null;
    }

    public static final String trimLeadingSeparator(String path, char separator) {
        if (path != null) {
            String p = normalise(path, separator);
            if (p.length() > 1) {
                return p.replaceAll("^\\" + separator + "+", "");
            } else {
                return p;
            }
        }
        return null;
    }
}

Related

  1. join(String separator, String... parts)
  2. join(String separator, String... strings)
  3. join(String separator, String... strings)
  4. join(String splitter, String... strs)
  5. join(String str[], String splitter)
  6. join(String... xs)
  7. join(T delimiter, List list)
  8. join2(String connector, String... strs)
  9. joinColumns(Iterable columns)