Java Array to String arrayToString(String array[], String separator)

Here you can find the source of arrayToString(String array[], String separator)

Description

array To String

License

Open Source License

Declaration

public static String arrayToString(String array[], String separator) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005-2012 Synopsys, Incorporated
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w ww .j a v  a2 s .  c o m
 * Synopsys, Inc - Initial implementation 
 *******************************************************************************/

import java.util.Arrays;
import java.util.List;

public class Main {
    public static String arrayToString(String array[], String separator) {
        return listToString(Arrays.asList(array), separator);
    }

    public static String arrayToString(String array[]) {
        return arrayToString(array, " ");
    }

    /**
    * Alternate to List.toString() that formats the list
    * as a simple string using a specified string as the
    * separator between elements.
    * @param l the list of strings to  concatenated into a single string.
    * @param sep the seperator to inserted between each list element.
    */
    public static String listToString(List<String> l, String sep) {
        if (l == null)
            return "";
        StringBuffer b = new StringBuffer();
        appendListToString(b, l, sep);
        return b.toString();
    }

    /**
    * Append a list to a string buffer placing a specified
    * string between each element.
    * @param b the string buffer to be filled in.
    * @param l the list of strings to be appended.
    * @param sep the seperator to inserted between each list element.
    */
    public static void appendListToString(StringBuffer b, List<String> l, String sep) {
        if (l != null) {
            for (String element : l) {
                b.append(sep);
                b.append(element);
            }
        }
    }
}

Related

  1. arrayToString(Object[] objs)
  2. arrayToString(Object[] objs, boolean stripPackageNames)
  3. arrayToString(Object[] objs, String separator)
  4. ArrayToString(Object[] row, StringBuilder sbf)
  5. arrayToString(Object[] subject)
  6. arrayToString(String format, double[] array)
  7. arrayToString(String in[])
  8. arrayToString(String values[])
  9. arrayToString(String[] a, String delim)