Java Array to String asStringOn(StringBuilder sb, Object[] items, String separator)

Here you can find the source of asStringOn(StringBuilder sb, Object[] items, String separator)

Description

Copies the array items onto the string builder each delimited by the separator.

License

BSD License

Parameter

Parameter Description
sb StringBuilder
items Object[]
separator String

Declaration

public static void asStringOn(StringBuilder sb, Object[] items, String separator) 

Method Source Code

//package com.java2s;
/**/*from   w w w. ja v  a  2s  .c  o  m*/
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
 */

import java.util.Iterator;

public class Main {
    /**
     * Copies the elements returned by the iterator onto the string buffer each
     * delimited by the separator.
     *
     * @param sb
     *            StringBuffer
     * @param iter
     *            Iterator
     * @param separator
     *            String
     */
    public static void asStringOn(StringBuffer sb, Iterator<?> iter, String separator) {

        if (!iter.hasNext()) {
            return;
        }

        sb.append(iter.next());

        while (iter.hasNext()) {
            sb.append(separator);
            sb.append(iter.next());
        }
    }

    /**
     * Copies the array items onto the string builder each delimited by the
     * separator. Does nothing if the array is null or empty.
     *
     * @param sb
     *            StringBuilder
     * @param items
     *            Object[]
     * @param separator
     *            String
     */
    public static void asStringOn(StringBuilder sb, Object[] items, String separator) {

        if (items == null || items.length == 0) {
            return;
        }

        sb.append(items[0]);

        for (int i = 1; i < items.length; i++) {
            sb.append(separator);
            sb.append(items[i]);
        }
    }
}

Related

  1. arrayToStringWithPrefix(T[] array, String splitter, String prefix)
  2. asString(final String[] array)
  3. asString(int[] objects, String separator)
  4. asString(String[] arrayParameter)
  5. asString(T[] objects)
  6. bitsToString(int[] arr)
  7. byteMapToString(Map map)
  8. bytesToString(byte[] buffer)
  9. convertArrayToString(double[] p)