Java Iterable to String toString(Iterable c, String delim, String prefix, String suffix)

Here you can find the source of toString(Iterable c, String delim, String prefix, String suffix)

Description

to String

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static String toString(Iterable c, String delim, String prefix, String suffix) 

Method Source Code

//package com.java2s;
/* /*  ww w.j  av a2s.  co m*/
 * @(#)StringUtil.java 1.0 2004-10-11
 *
 * Copyright 2005 UFIDA Software Co. Ltd. All rights reserved.
 * UFIDA PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

import java.util.Iterator;

public class Main {
    public static String toString(Object[] arr) {
        return toString(arr, ",");
    }

    @SuppressWarnings("unchecked")
    public static String toString(Object value, String delim) {
        if (value instanceof String) {
            return (String) value;
        } else {
            if (value.getClass().isArray()) {
                return toString((Object[]) value, delim);
            } else if (value instanceof Iterable) {
                return toString((Iterable) value, delim);
            } else {
                return value.toString();
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static String toString(Iterable c, String delim, String prefix, String suffix) {
        if (c == null) {
            return "null";
        }
        StringBuffer sb = new StringBuffer();
        Iterator it = c.iterator();
        int i = 0;
        while (it.hasNext()) {
            if (i++ > 0) {
                sb.append(delim);
            }
            sb.append(prefix + it.next() + suffix);
        }
        return sb.toString();
    }

    @SuppressWarnings("unchecked")
    public static String toString(Iterable c, String delim) {
        return toString(c, delim, "", "");
    }

    public static String toString(Object value) {
        return toString(value, ",");
    }

    public static String toString(Object[] arr, String delim) {
        if (arr == null) {
            return "null";
        } else {
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < arr.length; i++) {
                if (i > 0)
                    sb.append(delim);
                sb.append(arr[i]);
            }
            return sb.toString();
        }
    }
}

Related

  1. iterableToString(Iterable list)
  2. iterableToString(Iterable charSequenceIterable)
  3. iterableToString(Iterable itrbl)
  4. iterableToString(Iterable objects, int limit)
  5. toString(final Iterable iterable, final CharSequence delimiter)
  6. toString(Iterable objects, String sep)
  7. toString(Iterable it, String separator)
  8. toString(Iterable names)
  9. toString(Iterable iterable)