Java List Join join(List words, String iDelimiter)

Here you can find the source of join(List words, String iDelimiter)

Description

Join a list of words into a single string using the specified delimiter.

License

BSD License

Parameter

Parameter Description
words - array of words to join into a single string.
iDelimiter - the delimiter to use between each two words.

Return

A string constructed from the words separated with the delimiter.

Declaration

public static String join(List<? extends Object> words, String iDelimiter) 

Method Source Code

//package com.java2s;
// Licensed under the terms of the New BSD License. Please see associated LICENSE file for terms.

import java.util.List;

public class Main {
    /**/*from   ww  w . j a  va  2 s  .c om*/
     * Join a list of words into a single string using the specified delimiter.
     * 
     * @param words - array of words to join into a single string.
     * @param iDelimiter - the delimiter to use between each two words.
     * @return A string constructed from the words separated with the delimiter.
     */
    public static String join(List<? extends Object> words, String iDelimiter) {
        StringBuilder str = new StringBuilder();
        if (words.size() == 0) {
            return str.toString();
        }

        int i;
        for (i = 0; i < words.size() - 1; i++) {
            str.append(words.get(i));
            str.append(iDelimiter);
        }
        str.append(words.get(i));

        return str.toString();
    }

    /**
     * Join an array of words into a single string using the specified delimiter.
     * 
     * @param words - array of words to join into a single string.
     * @param iDelimiter - the delimiter to use between each two words.
     * @return A string constructed from the words separated with the delimiter.
     */
    public static String join(String[] words, String iDelimiter) {
        StringBuilder str = new StringBuilder();
        if (words.length == 0) {
            return str.toString();
        }

        int i;
        for (i = 0; i < words.length - 1; i++) {
            str.append(words[i]);
            str.append(iDelimiter);
        }
        str.append(words[i]);

        return str.toString();
    }
}

Related

  1. join(List items, String conjunction)
  2. join(List items, String separator)
  3. join(List list)
  4. join(List olist, String glue)
  5. join(List valueList, String separator)
  6. join(List list, T element)
  7. join(List _items, String _delim)
  8. join(List arr, String seperator)
  9. join(List items, char separator)