Java String Join join(String separator, String... parts)

Here you can find the source of join(String separator, String... parts)

Description

Join an arry of strings into a single using the specified delimiter.

License

Open Source License

Parameter

Parameter Description
iterable the subject
delimiter the delimiter

Return

the join

Declaration

public static String join(String separator, String... parts) 

Method Source Code

//package com.java2s;
/**//from   w w w  .j  a  va2s . c  o m
 * Licensed under Apache License v2. See LICENSE for more information.
 */

import java.util.Iterator;

public class Main {
    /**
     * Join an iterable of strings into a single using the specified delimiter.
     * 
     * @param iterable the subject
     * @param delimiter the delimiter
     * @return the join
     */
    public static String join(Iterable<? extends CharSequence> iterable,
            String delimiter) {
        Iterator<? extends CharSequence> iter = iterable.iterator();
        if (!iter.hasNext()) {
            return "";
        }
        StringBuilder buffer = new StringBuilder(iter.next());
        while (iter.hasNext()) {
            buffer.append(delimiter).append(iter.next());
        }
        return buffer.toString();
    }

    /**
     * Join an arry of strings into a single using the specified delimiter.
     * 
     * @param iterable the subject
     * @param delimiter the delimiter
     * @return the join
     */
    public static String join(String separator, String... parts) {
        StringBuilder builder = new StringBuilder();
        for (String part : parts) {
            if (builder.length() > 0) {
                builder.append(separator);
            }
            builder.append(part);
        }
        return builder.toString();
    }
}

Related

  1. join(String sep, Object... pieces)
  2. join(String separator, CharSequence... elements)
  3. join(String separator, Collection strings)
  4. join(String separator, List objects)
  5. join(String separator, Object... objects)
  6. join(String separator, String... strings)
  7. join(String separator, String... strings)
  8. join(String splitter, String... strs)
  9. join(String str[], String splitter)