Java Array to String toString(String[] array, String delimiter)

Here you can find the source of toString(String[] array, String delimiter)

Description

Converts an array of string to a string concatenation separated by the given delimiter.

License

Apache License

Parameter

Parameter Description
array the given string array to be converted to a string
delimiter the delimiter

Return

the string concatenation of the string array

Declaration

public static final String toString(String[] array, String delimiter) 

Method Source Code

//package com.java2s;
/**// w w  w.  jav  a2  s .  c o  m
 *  Copyright (c) 2009-2011 Misys Open Source Solutions (MOSS) and others
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 *  implied. See the License for the specific language governing
 *  permissions and limitations under the License.
 *
 *  Contributors:
 *    Misys Open Source Solutions - initial API and implementation
 *    -
 */

import java.util.List;

public class Main {
    /**
     * Converts a list of string to a string concatenation separated by the given delimiter.
     *
     * @param list           the given list to be converted to a string
     * @param delimiter      the delimiter
     * @param elementWrapper the String wrapper on each list element
     * @return the string concatenation of the list
     */
    public static final String toString(List<String> list, String delimiter, String elementWrapper) {
        if (list == null || list.size() == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            String value = list.get(i);
            sb.append(elementWrapper);
            sb.append(list.get(i));
            sb.append(elementWrapper);
            //Don't add the delimiter to last one
            if (i != list.size() - 1) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Converts a list of string to a string concatenation separated by the given delimiter.
     *
     * @param list      the given list to be converted to a string
     * @param delimiter the delimiter
     * @return the string concatenation of the list
     */
    public static final String toString(List<String> list, String delimiter) {
        if (list == null || list.size() == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i));
            //Don't add the delimiter to last one
            if (i != list.size() - 1) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Converts an array of string to a string concatenation separated by the given delimiter.
     *
     * @param array     the given string array to be converted to a string
     * @param delimiter the delimiter
     * @return the string concatenation of the string array
     */
    public static final String toString(String[] array, String delimiter) {
        if (array == null || array.length == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            sb.append(array[i]);
            //Don't add the delimiter to last one
            if (i != array.length - 1) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }
}

Related

  1. toString(Object[] array)
  2. toString(Object[] objects)
  3. toString(String arrayName, T[] targets)
  4. toString(String[] args, char color1, char color2)
  5. toString(String[] arguments)
  6. toString(String[] line)
  7. toString(String[] paramNames, Object[] params)
  8. toString(T[] array)
  9. toString(T[] array)