Aggregate String using the given char separator - Java java.lang

Java examples for java.lang:String Join

Description

Aggregate String using the given char separator

Demo Code

//package com.java2s;

public class Main {
    /**//from   w  ww  .  ja  v a 2s . c  o  m
     * Aggregate String using the given char separator
     *
     * @param messgaes
     *            - the string to aggregate
     * @param seperator
     *            - the separator to use between 2 strings
     * @return the string separated by the give separator
     */
    public static String aggregate(java.util.List<String> messages,
            char separator) {
        if (messages == null) {
            throw new IllegalArgumentException(
                    "The messages parameter can not be null");
        }
        StringBuffer fullString = new StringBuffer();
        if (messages.size() > 0) {
            for (String msg : messages) {
                fullString.append(msg).append(separator);
            }
            fullString.deleteCharAt(fullString.length() - 1);
        }
        return fullString.toString();
    }
}

Related Tutorials