Java Comma Separated List listToCommas(List list)

Here you can find the source of listToCommas(List list)

Description

Inserts a commas between each word in the given string with StringBuilder.

License

Open Source License

Parameter

Parameter Description
string The string to insert commas in.

Return

New string with commas.

Declaration

public static String listToCommas(List<String> list) 

Method Source Code

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

import java.util.List;

public class Main {
    /**/*from   w w  w  .j av a 2s. c  om*/
     * Inserts a commas between each word in the given string with
     * StringBuilder.
     * 
     * @param string
     *            The string to insert commas in.
     * @return New string with commas.
     */
    public static String listToCommas(List<String> list) {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < list.size(); i++) {
            String string = list.get(i);

            if ((i + 1) == list.size()) {
                builder.append(string);
            } else
                builder.append(string + ", ");
        }

        return builder.toString().trim();
    }
}

Related

  1. getStringListFromCommaSeparatedString(String input)
  2. humanReadableCommandLineOutput(List arguments)
  3. implodeCommaAnd(List list, String comma, String and)
  4. listToComma(List list)
  5. listToCommandLine(List commands)
  6. listToCommaSep(List strings)
  7. listToCommaSeparatedList(String prefix, List stringList)
  8. listToCommaSeparatedString(List list)
  9. listToString(List list, boolean doCommas)