Java List Concatenate concatWithSeparator(List list, Object delimiter)

Here you can find the source of concatWithSeparator(List list, Object delimiter)

Description

Concats a list of objects together using delimiter.

License

Open Source License

Declaration

public static String concatWithSeparator(List<?> list, Object delimiter) 

Method Source Code

//package com.java2s;
/**//w w  w .  j  ava  2  s  .c  o  m
 * Exhibit A - UIRF Open-source Based Public Software License.
 * 
 * The contents of this file are subject to the UIRF Open-source Based Public
 * Software License(the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * openelis.uhl.uiowa.edu
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
 * the specific language governing rights and limitations under the License.
 * 
 * The Original Code is OpenELIS code.
 * 
 * The Initial Developer of the Original Code is The University of Iowa.
 * Portions created by The University of Iowa are Copyright 2006-2008. All
 * Rights Reserved.
 * 
 * Contributor(s): ______________________________________.
 * 
 * Alternatively, the contents of this file marked "Separately-Licensed" may be
 * used under the terms of a UIRF Software license ("UIRF Software License"), in
 * which case the provisions of a UIRF Software License are applicable instead
 * of those above.
 */

import java.util.List;

public class Main {
    /**
     * Concats two strings together with the specified delimiter. Null
     * parameters are ignored and the delimiter is not used.
     */
    public static String concatWithSeparator(Object a, Object delimiter, Object b) {
        StringBuffer buf;

        buf = new StringBuffer();
        if (a != null)
            buf.append(a.toString().trim());
        if (b != null) {
            if (a != null)
                buf.append(delimiter);
            buf.append(b.toString().trim());
        }
        return buf.toString();
    }

    /**
     * Concats a list of objects together using delimiter.
     */
    public static String concatWithSeparator(List<?> list, Object delimiter) {
        StringBuffer buf;

        buf = new StringBuffer();
        for (Object i : list) {
            if (i != null) {
                if (buf.length() > 0)
                    buf.append(delimiter);
                buf.append(i.toString().trim());
            }
        }
        return buf.toString();
    }

    public static String trim(String result) {
        if (result != null) {
            result = result.trim();
            if (result.length() == 0)
                result = null;
        }
        return result;
    }

    public static String toString(Object o) {
        if (o != null)
            return o.toString();
        return "";
    }
}

Related

  1. concatStrings(List values, String separator)
  2. concatSuper(List collection1, List collection2)
  3. concatSynopsises( List prefixes, List ss)
  4. concatTwoList(List fromList, List toList)
  5. concatValues(List stringValues, boolean spaceSeparated)
  6. concatWithSeparator(List missingParameters, String separator)
  7. getConcatenatedStringFromList(List input, String delimiter)
  8. getConcatenateString(List codes)
  9. getConcatinatedRange(List numbers)