Java List to String toString(List list, char sep)

Here you can find the source of toString(List list, char sep)

Description

to String

License

LGPL

Declaration

public static String toString(List<?> list, char sep) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.List;

public class Main {
    private static final int INITIAL_STRING_SIZE = 128;
    public static final char DEFAULT_SEPARATOR = ',';
    public static final char DEFAULT_ESCAPE_CHAR = '\\';

    public static String toString(List<?> list) {
        return toString(list, DEFAULT_SEPARATOR, DEFAULT_ESCAPE_CHAR);
    }//from w  ww .ja  v  a  2 s. c om

    public static String toString(List<?> list, char sep) {
        return toString(list, sep, DEFAULT_ESCAPE_CHAR);
    }

    public static String toString(List<?> list, char sep, char esc) {
        if (sep == esc)
            throw new IllegalArgumentException("separator and escape char are equal");
        if (list == null)
            return null;

        StringBuilder strBuilder = new StringBuilder(INITIAL_STRING_SIZE);
        final int size = list.size();
        for (int i = 0; i < size; ++i) {
            if (i > 0)
                strBuilder.append(sep);
            Object obj = list.get(i);
            if (obj != null) {
                String str = obj.toString();
                if (str != null) {
                    final int nrChars = str.length();
                    for (int j = 0; j < nrChars; ++j) {
                        char c = str.charAt(j);
                        if (c == sep || c == esc)
                            strBuilder.append(esc);
                        strBuilder.append(c);
                    }
                }
            }
        }
        return strBuilder.toString();
    }
}

Related

  1. toString(List coll, char delimiter)
  2. toString(List list)
  3. toString(List list)
  4. toString(List list)
  5. toString(List list)
  6. toString(List list, String separator)
  7. toString(List objects)
  8. toString(List c)
  9. toString(List list)