Java examples for java.lang:String Join
implode a Collection to String with separator
//package com.java2s; import java.util.Collection; import java.util.Iterator; public class Main { public static void main(String[] argv) { Collection collection = java.util.Arrays.asList("asdf", "java2s.com"); String separator = "@"; System.out.println(implode(collection, separator)); }/*from w ww .j a va 2 s.c o m*/ public static String implode(Collection collection, String separator) { if (collection == null) return null; if (collection.size() == 0) return ""; Iterator iter = collection.iterator(); StringBuffer result = new StringBuffer(iter.next().toString()); while (iter.hasNext()) { if (separator != null) result.append(separator); result.append(iter.next().toString()); } return result.toString(); } public static String implode(Object[] array, String separator) { if (array == null) return null; if (array.length == 0) return ""; StringBuffer result = new StringBuffer(array[0].toString()); for (int i = 1; i < array.length; i++) { if (separator != null) result.append(separator); result.append(array[i].toString()); } return result.toString(); } }