Java String from List stringList(String prefix, String suffix, String separator, Object... objects)

Here you can find the source of stringList(String prefix, String suffix, String separator, Object... objects)

Description

Constructs a list of items in a string form using a prefix and suffix to denote the start and end of the list and a separator string in between the items.

License

Open Source License

Parameter

Parameter Description
prefix the prefix for the concatenated string.
suffix the suffix for the concatenated string.
separator the separator between the elements.
objects the array of the elements.

Return

a concatenated string of the elements in the array.

Declaration

public static String stringList(String prefix, String suffix, String separator, Object... objects) 

Method Source Code


//package com.java2s;
import java.util.List;

public class Main {
    /**// ww w . java2 s  .com
     * Constructs a list of items in a string form using a prefix and suffix to denote the start and end of the list and
     * a separator string in between the items. For example, with a prefix of '(', a suffix of ')', a separator of ','
     * and a list ["a", "b", "c"] it would generate the string "(a,b,c)"
     *
     * @param prefix    the prefix for the concatenated string.
     * @param suffix    the suffix for the concatenated string.
     * @param separator the separator between the elements.
     * @param objects   the array of the elements.
     * @return a concatenated string of the elements in the array.
     */
    public static String stringList(String prefix, String suffix, String separator, Object... objects) {
        StringBuilder builder = new StringBuilder(prefix);
        for (int i = 0; i < objects.length; i++) {
            builder.append(objects[i].toString());
            if (i < objects.length - 1) {
                builder.append(separator);
            }
        }
        builder.append(suffix);
        return builder.toString();
    }

    public static String stringList(Object[] objects) {
        return stringList("[", "]", ",", objects);
    }

    public static String stringList(List<?> objects) {
        if (objects == null) {
            return "";
        }
        return stringList(objects.toArray());
    }
}

Related

  1. stringFromLines(List lines)
  2. stringFromList(List list)
  3. stringfyList(List ls)
  4. stringify(List entries)
  5. stringifyList(List items)
  6. stringList2String(final String suffix, final List list)
  7. stringList2String(List list, String separator)
  8. stringListEquals(List left, List right)
  9. stringListString(List list)