Java List Join concatAndJoin(String sep, List data, String prefix, String suffix)

Here you can find the source of concatAndJoin(String sep, List data, String prefix, String suffix)

Description

Concat a prefix and suffix to each element in a list, and join with a separator.

License

Open Source License

Parameter

Parameter Description
sep separator between list elements
data list of strings
prefix prefix concatenated before each element in the list
suffix suffix concatenated after each element in the list

Declaration

public static String concatAndJoin(String sep, List<String> data,
        String prefix, String suffix) 

Method Source Code

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

import java.util.List;

public class Main {
    /**/*from   ww w  .  ja va2 s  .c  o m*/
     * Concat a prefix and suffix to each element in a list, and join with a separator.
     * For example, turn the list a b c into {a};{b};{c}
     * 
     * Equivalent to the following groovy code:
     * 
     * <pre>data.collect{ prefix + it + suffix }.join(sep)</pre>
     * 
     * @param sep separator between list elements
     * @param data list of strings
     * @param prefix prefix concatenated before each element in the list
     * @param suffix suffix concatenated after each element in the list 
     */
    public static String concatAndJoin(String sep, List<String> data,
            String prefix, String suffix) {
        StringBuilder builder = new StringBuilder();
        boolean first = true;

        for (String str : data) {
            if (first) {
                first = false;
            } else {
                builder.append(sep);
            }

            builder.append(prefix);
            builder.append(str);
            builder.append(suffix);
        }

        return builder.toString();
    }
}

Related

  1. concat(List tokens, int start, int end, String joiner)
  2. contains(List joinParams, int joinIndex)
  3. getJoinKeyValues(List tuple, List joinKeyIndices)
  4. join(char separator, List seqs)
  5. join(CharSequence delimiter, Iterable list)