Java Array Concatenate concatenateStrings(Object[] strings, String glueString)

Here you can find the source of concatenateStrings(Object[] strings, String glueString)

Description

Concatenates the strings into one big string, putting glueString between each pair.

License

Open Source License

Parameter

Parameter Description
strings a parameter
glueString a parameter

Return

the input strings concatenated into one string, adding the glueString between each pair.

Declaration

public static String concatenateStrings(Object[] strings, String glueString) 

Method Source Code

//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 "";
    }
}

Related

  1. concatenate(String[] strings, String sep)
  2. concatenate(T[] first, T[] second)
  3. concatenate2Arrays(T[] array1, T... array2)
  4. concatenateArray(Object[] srcOne, Object[] srcTwo)
  5. concatenateArrays(T[] first, T[]... rest)
  6. concatIntArrays(final int[] l1, final int[] l2)
  7. concatStrings(String[] strs, String delimiter)
  8. concatTwoStringArray(String[] first, String[] second)
  9. concatUniq(String[] part1, String[] part2)