Java String Join join(String glue, String... str)

Here you can find the source of join(String glue, String... str)

Description

Combine strings with a common string

License

Open Source License

Parameter

Parameter Description
glue a parameter
str a parameter

Return

A single string

Declaration

public static String join(String glue, String... str) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.List;

public class Main {
    /**/*  w w w.j  a v a  2s . com*/
     * Combine strings with a common string
     * @param glue
     * @param str
     * @return A single string
     */
    public static String join(String glue, String... str) {
        if (str.length > 0) {
            StringBuilder sb = new StringBuilder(str[0]);
            for (int i = 1; i < str.length; i++)
                sb.append(glue).append(str[i]);
            return sb.toString();
        } else
            return "";
    }

    /**
     * Combine a list of strings with a common string
     * @param glue
     * @param str
     * @return {@link #join(String,String...)}
     */
    public static String join(String glue, List<String> str) {
        return join(glue, str.toArray(new String[str.size()]));
    }
}

Related

  1. join(String delimiter, String... items)
  2. join(String delimiter, String... params)
  3. join(String delimiter, String... strings)
  4. join(String delimiter, T... array)
  5. join(String glue, Iterable items)
  6. join(String joiner, String... toJoin)
  7. join(String s, Object... objects)
  8. join(String sep, Object... pieces)
  9. join(String separator, CharSequence... elements)