Java List Join join(List strings)

Here you can find the source of join(List strings)

Description

Join the strings into one string.

License

Apache License

Parameter

Parameter Description
strings the string list to join

Return

the joined string

Declaration

public static String join(List<String> strings) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import java.util.List;

public class Main {
    /**//from   ww  w .  j a  va  2s  .c o  m
     * Join the {@code strings} into one string.
     * 
     * @param strings the string list to join
     * 
     * @return the joined string
     */
    public static String join(List<String> strings) {
        if (strings == null) {
            throw new NullPointerException("argument 'strings' cannot be null");
        }
        return join(strings.toArray(new String[strings.size()]));
    }

    /**
     * Join the {@code strings} into one string with {@code delimiter} in 
     * between.
     * 
     * @param strings the string list to join
     * @param delimiter the delimiter
     * 
     * @return the joined string
     */
    public static String join(List<String> strings, String delimiter) {
        if (strings == null) {
            throw new NullPointerException("argument 'strings' cannot be null");
        }
        return join(strings.toArray(new String[strings.size()]), delimiter);
    }

    /**
     * Join the {@code strings} into one string.
     * 
     * @param strings the string list to join
     * 
     * @return the joined string
     */
    public static String join(String[] strings) {
        return join(strings, null);
    }

    /**
     * Join the {@code strings} into one string with {@code delimiter} in 
     * between. It is similar to RegExpObject.test(string) in JavaScript.
     * 
     * @param strings the string list to join
     * @param delimiter the delimiter
     * 
     * @return the joined string
     */
    public static String join(String[] strings, String delimiter) {
        if (strings == null) {
            throw new NullPointerException("argument 'strings' cannot be null");
        }

        StringBuilder sb = new StringBuilder();

        if (strings.length != 0) {
            sb.append(strings[0]);
            for (int i = 1, iEnd = strings.length; i < iEnd; i++) {
                if (delimiter != null) {
                    sb.append(delimiter);
                }
                sb.append(strings[i]);
            }
        }

        return sb.toString();
    }
}

Related

  1. join(List p_sStrList, String p_sDelimiter)
  2. join(List parts, String separator)
  3. join(List paths)
  4. join(List s, String delim)
  5. join(List s, String sep)
  6. join(List strings)
  7. join(List strings)
  8. join(List strings, String delim)
  9. join(List strings, String delimiter)