Java List Join join(List stringsToJoin, char delimiter)

Here you can find the source of join(List stringsToJoin, char delimiter)

Description

Join the list of strings with the passed in delimiter.

License

Apache License

Parameter

Parameter Description
stringsToJoin the list of strings to join
delimiter the delimiter to use when joining the strings

Return

a string that represents the list of strings to join each separated by the delimiter character, null if stringsToJoin is null

Declaration

public final static String join(List<String> stringsToJoin, char delimiter) 

Method Source Code

//package com.java2s;
/*/*from  www . ja v  a  2  s.c  o  m*/
 * Copyright (c) 2013 Paul Mackinlay, Ramon Servadei 
 *  
 * 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.List;

public class Main {
    /**
     * Join the list of strings with the passed in delimiter. Any occurrence of the delimiter in the
     * strings is escaped with the delimiter, e.g.:
     * 
     * <pre>
     * If the delimiter char is ','
     * Joining "string with, comma" and "another string" -> "string with,, comma,another string"
     * </pre>
     * 
     * @param stringsToJoin
     *            the list of strings to join
     * @param delimiter
     *            the delimiter to use when joining the strings
     * @return a string that represents the list of strings to join each separated by the delimiter
     *         character, <code>null</code> if stringsToJoin is <code>null</code>
     */
    public final static String join(List<String> stringsToJoin, char delimiter) {
        if (stringsToJoin == null) {
            return null;
        }

        StringBuilder sb = new StringBuilder(stringsToJoin.size() * 30);
        for (int i = 0; i < stringsToJoin.size(); i++) {
            if (i > 0) {
                sb.append(delimiter);
            }
            addToBuilderAndEscapeDelimiter(stringsToJoin.get(i), delimiter, sb);
        }
        return sb.toString();
    }

    /**
     * Add the string to the builder and for any occurrence of the escapeChar, the escapeChar is
     * doubled, e.g.:
     * 
     * <pre>
     * If the escape char is ','
     * "string with, comma" -> "string with,, comma"
     * </pre>
     */
    private static void addToBuilderAndEscapeDelimiter(String string, char escapeChar, StringBuilder sb) {
        if (string == null) {
            sb.append((String) null);
            return;
        }

        char[] chars = string.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == escapeChar) {
                sb.append(escapeChar);
            }
            sb.append(chars[i]);
        }
    }
}

Related

  1. join(List strings, String separator)
  2. join(List strings, String separator)
  3. join(List strings, String separator)
  4. join(List strings, String separator)
  5. join(List strings, String separator)
  6. join(List strList, char separator)
  7. join(List strList, String sep)
  8. join(List tokens, String sep)
  9. join(List tokens, String separator)