Java List Implode implode(List tokens, char separator, char escapeCharacter)

Here you can find the source of implode(List tokens, char separator, char escapeCharacter)

Description

implode

License

Open Source License

Declaration

public static String implode(List<String> tokens, char separator, char escapeCharacter) 

Method Source Code

//package com.java2s;
/*//from w  w w  . ja  v a  2 s .  com
 * Copyright (c) 2014, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Eike Stepper - initial API and implementation
 *    Yatta Solutions - [466264] Enhance UX in simple installer
 */

import java.util.List;

public class Main {
    public static String implode(List<String> tokens, char separator) {
        return implode(tokens, separator, '\\');
    }

    public static String implode(List<String> tokens, char separator, char escapeCharacter) {
        String escapeString = Character.toString(escapeCharacter);
        String escapeString2 = escapeString + escapeString;

        String separatorString = Character.toString(separator);
        String separatorString2 = escapeString + separatorString;

        StringBuilder builder = new StringBuilder();
        boolean firstTime = true;

        for (String token : tokens) {
            if (firstTime) {
                firstTime = false;
            } else {
                builder.append(separator);
            }

            if (token != null) {
                token = token.replace(escapeString, escapeString2);
                token = token.replace(separatorString, separatorString2);
                builder.append(token);
            }
        }

        return builder.toString();
    }
}

Related

  1. implode(List elements, String separator)
  2. implode(List list, String delimiter)
  3. implode(List list, String glue)
  4. implode(List list, String glue)
  5. implode(List strings, String separator)
  6. implode(String glue, List list)
  7. implode(String split, List list)
  8. implodeList(final List list, final String glue)