Java Array Join join(String[] args)

Here you can find the source of join(String[] args)

Description

Merge the given strings, using a space between each.

License

Open Source License

Parameter

Parameter Description
args An array of Strings to merge.

Return

The given strings concatenated together with a space between each.

Declaration

public static String join(String[] args) 

Method Source Code

//package com.java2s;

import java.util.*;

public class Main {
    /**/*from w  w w.ja v a2s.com*/
     * Merge the given strings, using a space between each.
     *
     * @param args An array of Strings to merge.
     * @return The given strings concatenated together with a
     *         space between each.
     */
    public static String join(String[] args) {
        return join(" ", args);
    }

    /**
     * Merge the given strings, using  the given delimiter between each.
     *
     * @param delimiter The delimiter.
     * @param args      An array of Strings to merge.
     * @return The given strings concatenated together with the delimiter between each.
     */
    public static String join(String delimiter, Object[] args) {
        return join(delimiter, args, false);
    }

    /**
     * Merge the given strings, using  the given delimiter between each.
     *
     * @param delimiter          The delimiter.
     * @param args               An array of Strings to merge.
     * @param ignoreEmptyStrings Don't join empty strings
     * @return The given strings concatenated together with the delimiter between each.
     */
    public static String join(String delimiter, Object[] args, boolean ignoreEmptyStrings) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < args.length; i++) {
            if (ignoreEmptyStrings && ((args[i] == null) || (args[i].toString().length() == 0))) {
                continue;
            }
            if (i > 0) {
                sb.append(delimiter);
            }
            sb.append(args[i].toString());
        }
        return sb.toString();
    }

    /**
     * Merge the given strings, using  the given delimiter between each.
     *
     * @param delimiter The delimiter.
     * @param args      A List of objects whose toString value are merged.
     * @return The given object.toString values concatenated together with the delimiter between each.
     */
    public static String join(String delimiter, List args) {
        return join(delimiter, listToStringArray(args));
    }

    /**
     * Merge the given strings, using  the given delimiter between each.
     *
     * @param delimiter          The delimiter.
     * @param args               A List of objects whose toString value are merged.
     * @param ignoreEmptyStrings Should ignore empty strings
     * @return The given object.toString values concatenated together with the delimiter between each.
     */
    public static String join(String delimiter, List args, boolean ignoreEmptyStrings) {
        return join(delimiter, listToStringArray(args), ignoreEmptyStrings);
    }

    /**
     * Convert the list of objects to a list of strings.
     *
     * @param l List of objects
     * @return List of strings.
     */
    public static List toString(List l) {
        List stringList = new ArrayList();
        for (int i = 0; i < l.size(); i++) {
            stringList.add(l.get(i).toString());
        }
        return stringList;
    }

    /**
     * Create a string representation of the given array
     *
     * @param array array to print
     * @return array as a String
     */
    public static String toString(Object[] array) {
        StringBuffer buf = new StringBuffer();
        buf.append(": ");
        for (int i = 0; i < array.length; i++) {
            buf.append("[");
            buf.append(i);
            buf.append("]: ");
            buf.append((array[i] == null) ? "null" : array[i]);
            buf.append(" ");
        }
        return buf.toString();
    }

    /**
     * A utility method to an append to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null the we append to the StringBuffer the results of s1.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 object to append
     * @return StringBuffer with appended object
     */
    public static StringBuffer append(StringBuffer sb, Object s1) {
        if (sb == null) {
            sb = new StringBuffer();
        }
        sb.append((s1 == null) ? "null" : s1.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2) {
        sb = append(sb, s1);
        sb.append((s2 == null) ? "null" : s2.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3) {
        sb = append(sb, s1, s2);
        sb.append((s3 == null) ? "null" : s3.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4) {
        sb = append(sb, s1, s2, s3);
        sb.append((s4 == null) ? "null" : s4.toString());
        return sb;
    }

    /**
     * A utility method to do multiple appends to a StringBuffer.
     * If the given object is null the string "null" will be appended. If
     * non-null then we append to the StringBuffer the results of
     * sn.toString ();
     *
     * @param sb StringBuffer to append to (may be <code>null</code>)
     * @param s1 first object to append
     * @param s2 second object to append
     * @param s3 third object to append
     * @param s4 fourth object to append
     * @param s5 fifth object to append
     * @return StringBuffer with appended objects
     */
    public static StringBuffer append(StringBuffer sb, Object s1, Object s2, Object s3, Object s4, Object s5) {
        sb = append(sb, s1, s2, s3, s4);
        sb.append((s5 == null) ? "null" : s5.toString());
        return sb;
    }

    /**
     * Take the List of objects and return a String array
     * of the toString values of each object in the list.
     *
     * @param objectList The list of objects.
     * @return The array of the object string values.
     */
    public static String[] listToStringArray(List objectList) {
        String[] sa = new String[objectList.size()];
        for (int i = 0; i < objectList.size(); i++) {
            Object o = objectList.get(i);
            if (o != null) {
                sa[i] = o.toString();
            }
        }
        return sa;
    }
}

Related

  1. join(String separator, String[] strings)
  2. join(String[] a, String delimiter, Integer startIndex)
  3. join(String[] a, String[] b)
  4. join(String[] a1, String[] a2)
  5. join(String[] ar, String sep)
  6. join(String[] args, String delimiter)
  7. join(String[] array)
  8. join(String[] array)
  9. join(String[] array)