Java examples for java.lang:String Join
Joins elements of a List into a String, separated by specified separator
//package com.java2s; import java.util.Iterator; public class Main { /**//from w ww . j av a 2 s . co m * Joins elements of a List into a String, separated by specified separator * @param pieces List to join, e.g. ArrayList<String>(Arrays.asList("a", "b")) * @param separator String to separate elements of list, e.g. ", " * @return String of list elements joined by separator, e.g. "a, b" */ public static String join(Iterable<?> pieces, String separator) { Iterator<?> i = pieces.iterator(); if (!i.hasNext()) return ""; StringBuilder buf = new StringBuilder(); buf.append(String.valueOf(i.next()).trim()); // We already know that size > 1 while (i.hasNext()) { buf.append(separator).append(String.valueOf(i.next())); } return buf.toString(); } /** * Converts List<String> to String, joining with spaces * @param pieces List<String> to convert * @return String containing all items in arrayList */ public static String join(Iterable<?> pieces) { return join(pieces, " "); } }