Java Array Implode implode(final String[] array, final String delim)

Here you can find the source of implode(final String[] array, final String delim)

Description

Implodes a string array into a String.

License

Open Source License

Parameter

Parameter Description
array the array to implode
delim the delimiter to insert between values

Return

the imploded string

Declaration

public static String implode(final String[] array, final String delim) 

Method Source Code

//package com.java2s;

import java.util.Collection;

public class Main {
    /**// www . j  av  a 2s  .c o m
     * Implodes a string array into a String.
     * @param array the array to implode
     * @param delim the delimiter to insert between values
     * @param quoteStringsWithSpaces whether to surround strings that contain spaces with quotes or not
     * @return the imploded string
     */
    public static String implode(final String[] array, final String delim, final boolean quoteStringsWithSpaces) {
        final StringBuilder sb = new StringBuilder();
        for (int i = 0; i < array.length; i++) {
            if (i != 0 && delim != null) {
                sb.append(delim);
            }
            sb.append(quoteStringsWithSpaces ? smartQuote(array[i]) : array[i]);
        }
        return sb.toString();
    }

    /**
     * Implodes a string array into a String.
     * @param array the array to implode
     * @param delim the delimiter to insert between values
     * @return the imploded string
     */
    public static String implode(final String[] array, final String delim) {
        return implode(array, delim, false);
    }

    /**
     * Implodes a collection of strings into a String.
     * @param strings the collection to implode
     * @param delim the delimiter to insert between values
     * @param quoteStringsWithSpaces whether to surround strings that contain spaces with quotes or not
     * @return the imploded string
     */
    public static String implode(final Collection<String> strings, final String delim,
            final boolean quoteStringsWithSpaces) {
        return implode(strings.toArray(new String[strings.size()]), delim, quoteStringsWithSpaces);
    }

    /**
     * Implodes a collection of strings into a String.
     * @param strings the collection to implode
     * @param delim the delimiter to insert between values
     * @return the imploded string
     */
    public static String implode(final Collection<String> strings, final String delim) {
        return implode(strings, delim, false);
    }

    /**
     * Quotes string when spaces are detected
     * @param s string to quote
     * @return quoted string, or not
     */
    public static String smartQuote(String s) {
        if (s.contains(" ")) {
            return dumbQuote(s);
        } else {
            return s;
        }
    }

    /**
     * Quotes a string, escaping embedded slash or quotes
     * @param s string to quote
     * @return quoted string, always
     */
    public static String dumbQuote(String s) {
        return "\"" + s.replaceAll("\\\\", "\\\\\\\\").replaceAll("\"", "\\\\\"") + "\"";
    }
}

Related

  1. implode(double[] array, String separator)
  2. implode(final String[] pStrArray)
  3. implode(Object[] array, String separator)
  4. implode(Object[] ary, String delim)
  5. implode(String delim, Object[] objects)