Java Array Concatenate concat(Object[] array)

Here you can find the source of concat(Object[] array)

Description

Concatenates all elements of an array using the toString() -method.

License

Apache License

Parameter

Parameter Description
array the array containing the strings

Return

a concatenation of the string separated by spaces

Declaration

public static String concat(Object[] array) 

Method Source Code

//package com.java2s;
/*-------------------------------------------------------------------------+
|                                                                          |
| Copyright 2005-2011 The ConQAT Project                                   |
|                                                                          |
| 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.                                           |
+-------------------------------------------------------------------------*/

import java.util.Arrays;

import java.util.Iterator;

import java.util.Map;

public class Main {
    /** Line break. */
    public static final String CR = System.getProperty("line.separator");
    /** The empty string. */
    public static final String EMPTY_STRING = "";
    /** A space. */
    public static final String SPACE = " ";

    /**/*from  w  w  w .  j a v a 2 s . c om*/
     * Concatenates all elements of an iterable using the
     * <code>toString()</code>-method.
     * 
     * @param iterable
     *            the iterable
     * @return a concatenation, separated by spaces
     */
    public static String concat(Iterable<?> iterable) {
        return concat(iterable, SPACE);
    }

    /**
     * Concatenates all elements of an iterable using the
     * <code>toString()</code>-method, separating them with the given
     * <code>separator</code>.
     * 
     * @param iterable
     *            the iterable containing the strings
     * @param separator
     *            the separator to place between the strings, may be
     *            <code>null</code>
     * @return a concatenation of the string in the array or <code>null</code>
     *         if array was <code>null</code>. If array is of length 0 the empty
     *         string is returned.
     */
    public static String concat(Iterable<?> iterable, String separator) {
        if (iterable == null) {
            return null;
        }

        Iterator<?> iterator = iterable.iterator();

        if (!iterator.hasNext()) {
            return EMPTY_STRING;
        }

        if (separator == null) {
            separator = EMPTY_STRING;
        }

        StringBuilder builder = new StringBuilder();

        while (iterator.hasNext()) {
            builder.append(iterator.next());
            if (iterator.hasNext()) {
                builder.append(separator);
            }
        }

        return builder.toString();
    }

    /**
     * Concatenates all elements of an array using the <code>toString()</code>
     * -method.
     * 
     * @param array
     *            the array containing the strings
     * @return a concatenation of the string separated by spaces
     */
    public static String concat(Object[] array) {
        return concat(array, SPACE);
    }

    /**
     * Concatenates all elements of an array using the <code>toString()</code>
     * -method, separating them with the given <code>separator</code>.
     * 
     * @param array
     *            the array
     * @param separator
     *            the separator to place between the strings, may be
     *            <code>null</code>
     * @return a concatenation of the string in the array or <code>null</code>
     *         if array was <code>null</code>. If array is of length 0 the empty
     *         string is returned.
     */
    public static String concat(Object[] array, String separator) {
        if (array == null) {
            return null;
        }
        return concat(Arrays.asList(array), separator);
    }

    /** Concatenate two string arrays. */
    public static String[] concat(String[] array1, String[] array2) {
        String[] result = new String[array1.length + array2.length];
        System.arraycopy(array1, 0, result, 0, array1.length);
        System.arraycopy(array2, 0, result, array1.length, array2.length);
        return result;
    }

    /**
     * Create string representation of a map.
     */
    public static String toString(Map<?, ?> map) {
        return toString(map, EMPTY_STRING);
    }

    /**
     * Create string representation of a map.
     * 
     * @param map
     *            the map
     * @param indent
     *            a line indent
     */
    public static String toString(Map<?, ?> map, String indent) {
        StringBuilder result = new StringBuilder();
        Iterator<?> keyIterator = map.keySet().iterator();

        while (keyIterator.hasNext()) {
            result.append(indent);
            Object key = keyIterator.next();
            result.append(key);
            result.append(" = ");
            result.append(map.get(key));
            if (keyIterator.hasNext()) {
                result.append(CR);
            }
        }

        return result.toString();
    }
}

Related

  1. concat(final T[] elements, final T... elementsToAppend)
  2. concat(final T[] first, final T[] second)
  3. concat(float[] A, float[] B)
  4. concat(int[] first, int[] second)
  5. concat(int[]... arrays)
  6. concat(Object[] first, Object second)
  7. concat(String[] array, String separator)
  8. concat(String[] parts)
  9. concat(String[] strs)