Java List Join join(List l, String joiner)

Here you can find the source of join(List l, String joiner)

Description

join a string list into a single string

License

Open Source License

Parameter

Parameter Description
l list to be joined
joiner separator string

Return

joined string

Declaration

public static String join(List<String> l, String joiner) 

Method Source Code

//package com.java2s;
/*/*w  w  w.  j av  a  2  s . com*/
 * Copyright 2016 Jim O'Regan <jaoregan@tcd.ie>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

import java.util.List;

import java.util.Set;

public class Main {
    /**
     * join a string list into a single string
     * @param l list to be joined
     * @param joiner separator string
     * @return joined string
     */
    public static String join(List<String> l, String joiner) {
        StringBuilder sb = new StringBuilder();
        boolean start = true;
        for (String s : l) {
            if (start) {
                start = false;
            } else {
                sb.append(joiner);
            }
            sb.append(s);
        }
        return sb.toString();
    }

    /**
     * join a string set into a single string
     * @param l set to be joined
     * @param joiner separator string
     * @return joined string
     */
    public static String join(Set<String> l, String joiner) {
        StringBuilder sb = new StringBuilder();
        boolean start = true;
        for (String s : l) {
            if (start) {
                start = false;
            } else {
                sb.append(joiner);
            }
            sb.append(s);
        }
        return sb.toString();
    }

    /**
     * join a string array into a single string
     * @param a array to be joined
     * @param joiner separator string
     * @return joined string
     */
    public static String join(String[] a, String joiner) {
        StringBuilder sb = new StringBuilder();
        boolean start = true;
        for (String s : a) {
            if (start) {
                start = false;
            } else {
                sb.append(joiner);
            }
            sb.append(s);
        }
        return sb.toString();
    }
}

Related

  1. join(List array, String delim)
  2. join(List c, String seperator)
  3. join(List command, List args)
  4. join(List elements)
  5. join(List items, char separator)
  6. join(List lines, char delim)
  7. join(List list)
  8. join(List list)
  9. join(List list)