Java List Join join(String[] list, String separator)

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

Description

Join the elements of an array of strings with a separator.

License

Open Source License

Parameter

Parameter Description
list The input list
separator The separator between two elements

Return

The joined elements

Declaration

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

Method Source Code

//package com.java2s;
/**/* www . j  a v a  2s. c o m*/
 * Copyright (c) 2009 University of Rochester
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of the MIT/X11 license. The text of the
 * license can be found at http://www.opensource.org/licenses/mit-license.php and copy of the license can be found on the project
 * website http://www.extensiblecatalog.org/.
 *
 */

import java.util.Arrays;

import java.util.List;

public class Main {
    /**
     * Join the elements of an array of strings with a separator.
     * @param list The input list
     * @param separator The separator between two elements
     * @return The joined elements
     */
    public static String join(String[] list, String separator) {
        return join(Arrays.asList(list), separator);
    }

    /**
     * Join the elements of a list with a separator.
     * @param list The input list
     * @param separator The separator between two elements
     * @return The joined elements
     */
    public static String join(List list, String separator) {
        StringBuilder sb = new StringBuilder();
        String sep = "";
        for (Object member : list) {
            sb.append(sep).append(member.toString());
            sep = separator;
        }
        return sb.toString();
    }
}

Related

  1. join(String separator, List parts)
  2. join(String separator, List parts)
  3. join(String separator, List parts)
  4. join(String sign, List src)
  5. join(String[] elementList, String separator)
  6. join(T delimiter, List list)
  7. join(T element, List otherElements)
  8. joinAndQuote(char joinChar, char quoteChar, List strings)
  9. joinArray(List list, String separator)