Java List Implode implode(List elements, String separator)

Here you can find the source of implode(List elements, String separator)

Description

Implode a list of elements into a single string, with a specified separator.

License

Open Source License

Parameter

Parameter Description
elements The elements to implode.
separator The separator to use.

Return

The result string.

Declaration

public static String implode(List<String> elements, String separator) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**/*from ww w. ja v a 2 s .com*/
     * Implode a list of elements into a single string, with a specified separator.
     *
     * @param elements The elements to implode.
     * @param separator The separator to use.
     *
     * @return The result string.
     */
    public static String implode(List<String> elements, String separator) {
        // Create a string builder
        StringBuilder sb = new StringBuilder();

        // Append each element
        for (String element : elements) {
            // Make sure the element isn't empty
            if (element.trim().length() == 0)
                continue;

            // Prefix the separator if it isn't the first element
            if (sb.length() > 0)
                sb.append(separator);

            // Append the element
            sb.append(element);
        }

        // Return the result
        return sb.toString();
    }

    /**
     * Implode two lists of elements into a single string, with a specified separator.
     *
     * @param elements The first list of elements to implode.
     * @param otherElements The second list of elements to implode.
     * @param separator The separator to use.
     *
     * @return The result string.
     */
    public static String implode(List<String> elements, List<String> otherElements, String separator) {
        // Combine the lists
        List<String> combined = new ArrayList<>();
        combined.addAll(elements);
        combined.addAll(otherElements);

        // Implode and return the result
        return implode(combined, separator);
    }

    /**
     * Implode two elements into a single string, with a specified separator.
     *
     * @param element The first element to implode.
     * @param otherElement The second element to implode.
     * @param separator The separator to use.
     *
     * @return The result string.
     */
    public static String implode(String element, String otherElement, String separator) {
        // Combine the lists
        List<String> combined = new ArrayList<>();
        combined.add(element);
        combined.add(otherElement);

        // Implode and return the result
        return implode(combined, separator);
    }
}

Related

  1. implode(Collection list, String separator)
  2. implode(final List pieces, final String glue)
  3. implode(List list, String deliminator)
  4. implode(List objs, String delim)
  5. implode(List list, String delimiter)
  6. implode(List list, String glue)
  7. implode(List list, String glue)
  8. implode(List strings, String separator)