Java List Join join(String _delimiter, List _strings)

Here you can find the source of join(String _delimiter, List _strings)

Description

Combines the Strings in _string using _delimiter.

License

Open Source License

Parameter

Parameter Description
_delimiter delimiting string
_strings strings to join

Return

null if _strings is null, concatenated string otherwise

Declaration

public static String join(String _delimiter, List<String> _strings) 

Method Source Code

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

import java.util.Arrays;
import java.util.List;

public class Main {
    /**/*  w w  w  . j av  a  2 s.c  o m*/
     * Combines the Strings in _string using _delimiter.
     * @param _delimiter delimiting string
     * @param _strings strings to join
     * @return null if _strings is null, concatenated string otherwise
     */
    public static String join(String _delimiter, List<String> _strings) {
        if (_strings == null) {
            return null;
        }
        if (_delimiter == null) {
            _delimiter = "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < _strings.size(); i++) {
            sb.append(_strings.get(i));
            if (i < _strings.size() - 1) { // only append delimiter if this is not the last token
                sb.append(_delimiter);
            }
        }
        return sb.toString();
    }

    /**
     * Combines the Strings in _string using _delimiter.
     * @param _delimiter delimiting string
     * @param _strings string to join
     * @return null if _strings is null, concatenated string otherwise
     */
    public static String join(String _delimiter, String[] _strings) {
        return join(_delimiter, Arrays.asList(_strings));
    }
}

Related

  1. join(List tokens, String delimiter)
  2. join(List values, String separator)
  3. join(List... lists)
  4. join(List... ls)
  5. join(List... elements)
  6. join(String delimiter, List strings)
  7. join(String joiner, List joinees)
  8. join(String joinString, List strings)
  9. join(String joint, List elements)