Java Array Join join(String[] tokens, String separator)

Here you can find the source of join(String[] tokens, String separator)

Description

Join all tokens dividing by a separator

License

LGPL

Parameter

Parameter Description
tokens the string tokens
separator the separator to use between all tokens

Return

a string will all tokens separated by the defined separator

Declaration

public static String join(String[] tokens, String separator) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.List;

public class Main {
    /**/*from   w  w w.j a v  a 2  s  .  co  m*/
     * Join all tokens dividing by a separator
     * 
     * @param tokens
     *          the string tokens
     * @param separator
     *          the separator to use between all tokens
     * @return a string will all tokens separated by the defined separator
     */
    public static String join(String[] tokens, String separator) {
        StringBuilder history = new StringBuilder();
        if (tokens.length > 0) {
            history.append(tokens[0]);
        }
        for (int i = 1; i < tokens.length; i++) {
            history.append(separator).append(tokens[i]);
        }
        return history.toString();
    }

    public static String join(List<String> tokens, String separator) {
        StringBuilder history = new StringBuilder();
        if (!tokens.isEmpty()) {
            history.append(tokens.get(0));
        }
        for (int i = 1; i < tokens.size(); i++) {
            history.append(separator).append(tokens.get(i));
        }
        return history.toString();
    }
}

Related

  1. join(String[] strings, String seperator)
  2. join(String[] strParts, String glue)
  3. join(String[] strs)
  4. join(String[] strs, String glue, int start, int end)
  5. join(String[] tokens, String delimiter)
  6. join(String[] x)
  7. join(String[]... arrays)
  8. join(String[]... values)
  9. join(T[] a, T[] b)