Here you can find the source of concatenateStrings(Object[] strings, String glueString)
strings
into one big string, putting glueString
between each pair.
Parameter | Description |
---|---|
strings | a parameter |
glueString | a parameter |
glueString
between each pair.
public static String concatenateStrings(Object[] strings, String glueString)
//package com.java2s; /*-//from w w w.j a v a 2s. c o m * Copyright (C) 2006-2008 Erik Larsson * * 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.List; public class Main { /** * Concatenates the <code>strings</code> into one big string, putting * <code>glueString</code> between each pair. Example:<br/> * <code>concatenateStrings(new String[] {"joe", "lisa", "bob"}, * " and ");</code> yields the string "joe and lisa and bob". * * @param strings * @param glueString * @return the input strings concatenated into one string, adding the * <code>glueString</code> between each pair. */ public static String concatenateStrings(Object[] strings, String glueString) { if (strings.length > 0) { StringBuilder sb = new StringBuilder(strings[0].toString()); for (int i = 1; i < strings.length; ++i) sb.append(glueString).append(strings[i].toString()); return sb.toString(); } else return ""; } public static String concatenateStrings(List<? extends Object> strings, String glueString) { if (strings.size() > 0) { StringBuilder sb = new StringBuilder(); boolean first = true; for (Object s : strings) { if (!first) sb.append(glueString); else first = false; sb.append(s.toString()); } return sb.toString(); } else return ""; } }