join Collection<?> to String - Java java.lang

Java examples for java.lang:String Join

Description

join Collection<?> to String

Demo Code

//package com.java2s;
import java.util.Collection;
import java.util.Iterator;

public class Main {
    public static String join(Collection<?> collection,
            String collectionPrefix, String prefix, String postfix,
            String collectionPostfix) {
        String result = collectionPrefix;
        if (collection.size() == 0) {
            return "";
        }//from w w  w . ja va 2 s  .  c  om
        Iterator<?> iterator = collection.iterator();
        for (int i = 0; i < collection.size(); i++) {
            result += prefix + iterator.next();
            if (i != collection.size() - 1) {
                result += postfix;
            }
        }
        result += collectionPostfix;
        return result;

    }
}

Related Tutorials