Java Array Merge mergeArrayIntoString(final Object[] array, String middleDelimiter, String lastDelimiter)

Here you can find the source of mergeArrayIntoString(final Object[] array, String middleDelimiter, String lastDelimiter)

Description

merge Array Into String

License

Open Source License

Declaration

public static String mergeArrayIntoString(final Object[] array, String middleDelimiter, String lastDelimiter) 

Method Source Code

//package com.java2s;
/*/*ww w . jav  a 2s. c o  m*/
 * Copyright 2001-2008 Aqris Software AS. All rights reserved.
 * 
 * This program is dual-licensed under both the Common Development
 * and Distribution License ("CDDL") and the GNU General Public
 * License ("GPL"). You may elect to use one or the other of these
 * licenses.
 */

public class Main {
    public static String mergeArrayIntoString(final Object[] array, String middleDelimiter, String lastDelimiter) {

        if (array == null) {
            return null;
        }
        if (middleDelimiter == null) {
            middleDelimiter = "";
        }
        if (lastDelimiter == null) {
            lastDelimiter = middleDelimiter;
        }

        final StringBuffer str = new StringBuffer();
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                if (i == array.length - 1) {
                    str.append(lastDelimiter);
                } else {
                    str.append(middleDelimiter);
                }
            }
            str.append(array[i].toString());
        }

        return str.toString();
    }

    public static String mergeArrayIntoString(Object[] array, String delimiter) {
        return mergeArrayIntoString(array, delimiter, delimiter);
    }

    public static String toString(final boolean[] array) {
        String result = "[";
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                result += ", ";
            }
            result += array[i];
        }

        return result + "]";
    }

    public static String toString(final int[] array) {
        String result = "[";
        for (int i = 0; i < array.length; i++) {
            if (i > 0) {
                result += ", ";
            }
            result += array[i];
        }

        return result + "]";
    }
}

Related

  1. mergeArgs(String[] args)
  2. mergeArray(byte[] arr1, byte[] arr2)
  3. mergeArray(final Object[] dest, final Object[]... arrays)
  4. mergeArray(String[] a, String[] b)
  5. mergeArray(T[] objs, String concatenator)
  6. mergeArrayObject(Object[] buf1, Object[] buf2)
  7. mergeArrays(byte[] buf1, byte[] buf2)
  8. mergeArrays(byte[] first, byte[]... more)
  9. mergeArrays(final byte[] buf1, final byte[] buf2)