Java String Join join(Enumeration values, CharSequence separator)

Here you can find the source of join(Enumeration values, CharSequence separator)

Description

Concatenates all of the strings in an Enumeration.

License

Open Source License

Parameter

Parameter Description
values Strings to concatenate
separator Use this as a separator between the strings.

Return

Concatenation of all the strings in values , with separator between adjacent values.

Declaration

public static String join(Enumeration values, CharSequence separator) 

Method Source Code


//package com.java2s;
import java.util.*;

public class Main {
    /**// w  w  w  .j a v a 2 s .c  o  m
     * Concatenates all of the strings in an array.
     * @param values                   Strings to concatenate
     * @param separator                Use this as a separator between the
     *                                 strings.
     * @return                         Concatenation of all the strings
     *                                 in {@code values}, with
     *                                 {@code separator} between adjacent
     *                                 values.
     */
    public static String join(CharSequence[] values, CharSequence separator) {
        StringBuilder builder = new StringBuilder(joinedLength(values, separator));
        CharSequence prefix = "";
        for (int i = 0; i < values.length; i++) {
            builder.append(prefix);
            builder.append(values[i]);
            prefix = separator;
        }
        return builder.toString();
    }

    /**
     * Concatenates all of the values in an array of objects.
     * @param values                   Objects whose string values should be
     *                                 concatenated
     * @param separator                Use this as a separator between the
     *                                 values.
     * @return                         Concatenation of all the values
     *                                 in {@code values}, with
     *                                 {@code separator} between adjacent
     *                                 values.
     */
    public static String join(Object[] values, CharSequence separator) {
        StringBuilder builder = new StringBuilder();
        CharSequence prefix = "";
        for (int i = 0; i < values.length; i++) {
            builder.append(prefix);
            builder.append(values[i].toString());
            prefix = separator;
        }
        return builder.toString();
    }

    /**
     * Concatenates all of the strings in an iterable.
     * @param values                   Strings to concatenate
     * @param separator                Use this as a separator between the
     *                                 strings.
     * @return                         Concatenation of all the strings
     *                                 in {@code values}, with
     *                                 {@code separator} between adjacent
     *                                 values.
     */
    public static String join(Iterable values, CharSequence separator) {
        StringBuilder builder = new StringBuilder();
        CharSequence prefix = "";
        for (Object o : values) {
            builder.append(prefix);
            builder.append(o.toString());
            prefix = separator;
        }
        return builder.toString();
    }

    /**
     * Concatenates all of the strings in an Enumeration.
     * @param values                   Strings to concatenate
     * @param separator                Use this as a separator between the
     *                                 strings.
     * @return                         Concatenation of all the strings
     *                                 in {@code values}, with
     *                                 {@code separator} between adjacent
     *                                 values.
     */
    public static String join(Enumeration values, CharSequence separator) {
        StringBuilder builder = new StringBuilder();
        CharSequence prefix = "";
        while (values.hasMoreElements()) {
            builder.append(prefix);
            builder.append(values.nextElement().toString());
            prefix = separator;
        }
        return builder.toString();
    }

    /**
     * Concatenates all of the strings in a Set, in sorted order.
     * @param set                      Contains string values to concatenate.
     * @param separator                Use this as a separator between the
     *                                 strings.
     * @return                         Concatenation of all the strings
     *                                 in {@code set}, sorted alphabetically
     *                                 with {@code separator} between adjacent
     *                                 values.
     */
    public static String join(Set<String> set, CharSequence separator) {
        ArrayList<String> names = new ArrayList<String>();
        names.addAll(set);
        Collections.sort(names);
        return join(names, separator);
    }

    /**
     * Utility method used by {@code join}: computes the total space
     * needed to join strings, in order to avoid reallocation in the
     * StringBuilder used for the result.
     * @param values                   Strings to concatenate
     * @param separator                Separator between values.
     * @return                         Total number of characters needed to
     *                                 hold all the strings in
     *                                 {@code values}, with
     *                                 {@code separator} between adjacent
     *                                 values.
     */
    public static int joinedLength(CharSequence[] values, CharSequence separator) {
        int length = 0, separatorLength = 0;
        for (int i = 0; i < values.length; i++) {
            length += values[i].length() + separatorLength;
            separatorLength = separator.length();
        }
        return length;
    }
}

Related

  1. Join(ArrayList list, String delimiter)
  2. join(boolean endSlash, Collection paths)
  3. join(Collection collection, String separator)
  4. join(Collection items, String delimiter)
  5. join(Enumeration enumeration, String s)
  6. join(final Collection input, final String link)
  7. join(final String sep, final Iterable strs)
  8. join(final String separator, final Collection objs)
  9. join(final String separator, final String... strings)