Java List Concatenate concatenateStrings(List strings, String separator)

Here you can find the source of concatenateStrings(List strings, String separator)

Description

Concatenate the Strings in the List, putting the separator between each but not after the last.

License

Open Source License

Parameter

Parameter Description
strings a parameter
separator a parameter

Declaration

public static String concatenateStrings(List<String> strings, String separator) 

Method Source Code

//package com.java2s;
/**/*from  ww w.j av  a  2s  .  c  o  m*/
 * Copyright (c) 2012 eXtensible Catalog Organization
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the MIT/X11 license. The text of the license can be
 * found at http://www.opensource.org/licenses/mit-license.php.
 */

import java.util.*;

public class Main {
    /**
     * Concatenate the Strings in the List, putting the separator between each but not after the last.
     * @param strings
     * @param separator
     * @return
     */
    public static String concatenateStrings(List<String> strings, String separator) {

        String result = "";

        if (strings != null && !strings.isEmpty()) {

            StringBuilder sb = new StringBuilder();
            for (String s : strings) {

                sb.append(s).append(separator);

            }

            result = sb.toString();
            result = result.substring(0, result.length() - separator.length());

        }

        return result;
    }
}

Related

  1. concatenateList(List values)
  2. concatenateList(List list, String separator)
  3. concatenateListOfStrings(List list, String separator)
  4. concatenateLists(List... lists)
  5. concatenateParameters(List parameters)
  6. concatenateUniqueLists(List first, List second)
  7. concatGenericList(List list, String delimiter)
  8. concatinateStrings(List strList, String delim)
  9. concatIntegersToRanges( List damages)