Java - Write code to join list of string to String

Requirements

Write code to join list of string to String

Demo

//package com.book2s;
import java.util.List;

public class Main {
    public static void main(String[] argv) {
        String xs = "book2s.com";
        System.out.println(join(xs));
    }/* w  ww. ja v  a 2s  .  c  o  m*/

    public static String join(String... xs) {
        StringBuilder sb = new StringBuilder();
        for (String x : xs)
            sb.append(x);
        return sb.toString();
    }

    public static String join(List<String> xs) {
        StringBuilder sb = new StringBuilder();
        for (String x : xs)
            sb.append(x);
        return sb.toString();
    }

    public static String join(List<String> xs, String sep) {
        int sepLen = sep.length();
        StringBuilder sb = new StringBuilder();
        for (String x : xs)
            sb.append(x).append(sep);
        return sb.length() > 0 ? sb.delete(sb.length() - sepLen,
                sb.length()).toString() : "";
    }
}

Related Exercise