Java String Implode implode(char delimiter, char escape, String... input)

Here you can find the source of implode(char delimiter, char escape, String... input)

Description

Combine multiple Strings into one using the specified delimiter and the specified escape character.

License

Open Source License

Parameter

Parameter Description
input a parameter
delimiter a parameter
escape a parameter

Declaration

public static String implode(char delimiter, char escape, String... input) 

Method Source Code

//package com.java2s;
// This package is part of the Spiralcraft project and is licensed under

public class Main {
    /**//  w w w.  j  av  a  2  s. com
     * Combine multiple Strings into one using the specified delimiter and
     *   the specified escape character. Occurrences of the delimiter and
     *   the escape character in the individual components will be escaped
     *   by prepending the escape character.
     * 
     * @param input
     * @param delimiter
     * @param escape
     * @return
     */
    public static String implode(char delimiter, char escape, String... input) {
        // ClassLog log=ClassLog.getInstance(StringUtil.class);
        boolean first = true;
        StringBuilder result = new StringBuilder();
        //log.fine("Imploding '"+delimiter+"','"+escape+"',"+ArrayUtil.format(input,"|",null));
        for (String str : input) {
            if (!first) {
                result.append(delimiter);
            } else {
                first = false;
            }
            result.append(escape(str, escape, Character.toString(delimiter)));
        }
        return result.toString();
    }

    /**
     * Escapes charsToEscape by preceeding them with the escape char. The
     *   escapeChar is automatically escaped.
     * 
     * @param input
     * @param escapeChar
     * @param charsToEscape
     * @return
     */
    public static String escape(String input, char escapeChar, String charsToEscape) {
        if (input == null) {
            return null;
        }
        final StringBuilder out = new StringBuilder();
        for (char c : input.toCharArray()) {
            if (charsToEscape.indexOf(c) > -1 || c == escapeChar) {
                out.append(escapeChar);
            }
            out.append(c);
        }
        return out.toString();
    }
}

Related

  1. implode(final char separator, final Object... array)
  2. implode(final String separator, final Iterable data)
  3. implode(Object strarr[], String delim)
  4. implode(Object[] data, String delimiter)