Java Array to String arrayToString(String[] strs)

Here you can find the source of arrayToString(String[] strs)

Description

Given an array of strings, return a comma-separated list of its elements.

License

Open Source License

Parameter

Parameter Description
strs Array of strings

Return

Empty string if strs.length is 0, comma separated list of strings otherwise

Declaration


public static String arrayToString(String[] strs) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from w  ww.  j  a v a 2s  .c o  m*/
     * Given an array of strings, return a comma-separated list of its elements.
     * 
     * @param strs
     *            Array of strings
     * @return Empty string if strs.length is 0, comma separated list of strings otherwise
     */

    public static String arrayToString(String[] strs) {
        if (strs.length == 0) {
            return "";
        }
        StringBuffer sbuf = new StringBuffer();
        sbuf.append(strs[0]);
        for (int idx = 1; idx < strs.length; idx++) {
            sbuf.append(",");
            sbuf.append(strs[idx]);
        }
        return sbuf.toString();
    }

    public static String toString(String[] content, String sign) {
        if (null == content) {
            return null;
        }

        sign = null == sign ? "," : sign;
        StringBuilder strBuilder = new StringBuilder();
        for (int i = 0; i < content.length; i++) {
            strBuilder.append(content[i]);
            if (i < content.length - 1) {
                strBuilder.append(sign);
            }
        }

        return strBuilder.toString();
    }
}

Related

  1. arrayToString(String[] str, String separator)
  2. arrayToString(String[] stringArray)
  3. arrayToString(String[] stringArray)
  4. arrayToString(String[] strings)
  5. arrayToString(String[] strs)
  6. arrayToString(String[] strs, String separator)
  7. arrayToString(String[] theArray)
  8. ArrayToString(String[] values)
  9. arrayToString(String[] values)