Java Collection Join join(Collection s, String delimiter)

Here you can find the source of join(Collection s, String delimiter)

Description

join

License

Open Source License

Declaration

public static String join(Collection<String> s, String delimiter) 

Method Source Code


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

import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(Collection<String> s, String delimiter) {
        return join(s, delimiter, false);
    }/*  w ww.j  av a  2s .  c o  m*/

    public static String join(Collection<String> s, String delimiter, boolean spaceAfterDelimiter) {
        StringBuilder sb = new StringBuilder();
        Iterator<String> iter = s.iterator();
        while (iter.hasNext()) {
            sb.append(iter.next());
            if (iter.hasNext()) {
                sb.append(delimiter);
                if (spaceAfterDelimiter)
                    sb.append(' ');
            }
        }
        return sb.toString();
    }

    public static String join(String[] s, String delimiter) {
        return join(s, delimiter, false);
    }

    public static String join(String[] s, String delimiter, boolean spaceAfterDelimiter) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length; i++) {
            sb.append(s[i]);
            if (i != s.length - 1) {
                sb.append(delimiter);
                if (spaceAfterDelimiter)
                    sb.append(' ');
            }
        }
        return sb.toString();
    }
}

Related

  1. join(Collection items, String join)
  2. join(Collection items, String prefix, String suffix, String itemPrefix, String itemSuffix)
  3. join(Collection list)
  4. join(Collection list, String conjunction)
  5. join(Collection packs)
  6. join(Collection s, String delimiter)
  7. join(Collection src, String delim)
  8. join(Collection strings)
  9. join(Collection strings, String delimiter)