Java String Array to String arrayAsString(String[] haystack)

Here you can find the source of arrayAsString(String[] haystack)

Description

build a string based on an array of strings, uses the default delimiter ', '

License

Open Source License

Parameter

Parameter Description
haystack the array of strings to use

Return

a string containing all of the elements of the array

Declaration

public static String arrayAsString(String[] haystack) 

Method Source Code

//package com.java2s;
/*//from  w w w  .j  a va  2  s .  com
 * Copyright (C) 2012 The Serval Project
 *
 * This file is part of the Serval Maps Map Index File Generator
 *
 * Serval Maps Map Index File Generator is free software; you can 
 * redistribute it and/or modify it under the terms of the GNU General
 * Public License as published by the Free Software Foundation; either 
 * version 3 of the License, or (at your option) any later version.
 *
 * This source code 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this source code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

public class Main {
    /**
     * build a string based on an array of strings, uses the default delimiter ', '
     * @param haystack the array of strings to use
     * @return a string containing all of the elements of the array
     */
    public static String arrayAsString(String[] haystack) {
        return arrayAsString(haystack, ", ");
    }

    /**
     * build a string based on an array of strings
     * @param haystack the array of strings to use
     * @param delimiter the delimiter to use between strings
     * @return a string containing all of the elements of the array
     */
    public static String arrayAsString(String[] haystack, String delimiter) {

        StringBuilder builder = new StringBuilder();

        for (String item : haystack) {
            builder.append(item + delimiter);
        }

        String value = builder.toString();

        return value.substring(0, value.length() - delimiter.length());

    }
}

Related

  1. arr2str(String[] arr, String sep)
  2. array2str(String[] array, String delim)
  3. array2str(String[] arrayname, int length)
  4. array2string(String array[])
  5. array2string(String[] s)
  6. arrayAsString(String[] stringArray)
  7. ArrayCombine(String[] array, String delimiter)
  8. arrayStringToArrayInt(final String[] as)
  9. arrayStringtoString(final String[] arrayString)