Java tutorial
//package com.java2s; /* * logic2j - "Bring Logic to your Java" - Copyright (C) 2011 Laurent.Tettoni@gmail.com * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ import java.util.*; public class Main { /** * Format a collection using it's element's {@link String#valueOf(Object)} method, but inserting a separator between * consecutive elements, and not surrounding the result by any braket, brace or parenthesis. * * @param theCollection The collection to format. Must not be null. * @param theSeparator The string used to interleave between consecutive elements. May be "" to pack elements together. Normally use a * space around, e.g. " OR ". If null, then the empty string is used. * @return A formatter string, never null. May span several lines depending on the element's toString() or on the separator value. * @throws IllegalArgumentException If coll is null. */ public static String formatSeparated(Collection<?> theCollection, String theSeparator) { if (theCollection == null) { throw new IllegalArgumentException("Cannot format null collection"); } String separator = theSeparator; if (separator == null) { separator = ""; } final StringBuilder sb = new StringBuilder(); for (final Iterator<?> iter = theCollection.iterator(); iter.hasNext();) { final String element = String.valueOf(iter.next()); sb.append(element); if (iter.hasNext()) { sb.append(separator); } } return sb.toString(); } /** * Format an array using it's element's {@link String#valueOf(Object)} method, but inserting a separator between consecutive * elements, and not surrounding the result by any braket, brace or parenthesis. * * @param theArray The array to format. Must not be null. * @param theSeparator The string used to interleave between consecutive elements. May be "" to pack elements together. Normally use a * space around, e.g. " OR ". If null, then the empty string is used. * @return A formatter string, never null. May span several lines depending on the element's toString() or on the separator value. * @throws IllegalArgumentException If coll is null. */ public static String formatSeparated(Object[] theArray, String theSeparator) { if (theArray == null) { throw new IllegalArgumentException("Cannot format null array"); } String separator = theSeparator; if (separator == null) { separator = ""; } final StringBuilder sb = new StringBuilder(); for (int i = 0; i < theArray.length; i++) { final String element = String.valueOf(theArray[i]); sb.append(element); if (i < theArray.length - 1) { sb.append(separator); } } return sb.toString(); } }