Java Collection Join join(AbstractCollection s, String delimiter)

Here you can find the source of join(AbstractCollection s, String delimiter)

Description

Join a Collection of Strings using a delimiter

License

Open Source License

Parameter

Parameter Description
s : Collection <String>
delimiter : String

Return

joined String

Declaration

public static String join(AbstractCollection<String> s, String delimiter) 

Method Source Code


//package com.java2s;
/*/*from   w ww .  j a v  a 2 s .co  m*/
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2013 BiBiServ Curator Team, http://bibiserv.cebitec.uni-bielefeld.de,
 * All rights reserved.
 *
 * The contents of this file are subject to the terms of the Common
 * Development and Distribution License("CDDL") (the "License"). You
 * may not use this file except in compliance with the License. You can
 * obtain a copy of the License at http://www.sun.com/cddl/cddl.html
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.  When distributing the software, include
 * this License Header Notice in each file.  If applicable, add the following
 * below the License Header, with the fields enclosed by brackets [] replaced
 *  by your own identifying information:
 *
 * "Portions Copyrighted  2013 BiBiServ Curator Team"
 *
 * Contributor(s): Jan Krueger
 *
 */

import java.util.AbstractCollection;

import java.util.Iterator;

public class Main {
    /**
     * Join a Collection of Strings using a delimiter
     *
     * @param s : Collection <String>
     * @param delimiter : String
     * @return joined String
     */
    public static String join(AbstractCollection<String> s, String delimiter) {
        if (s == null || s.isEmpty()) {
            return "";
        }
        Iterator<String> iter = s.iterator();
        StringBuilder builder = new StringBuilder(iter.next());
        while (iter.hasNext()) {
            builder.append(delimiter).append(iter.next());
        }
        return builder.toString();
    }

    /**
     * Join a collection of Strings using a delimiter
     *
     * @param s : Collection<String>
     * @return joined String
     */
    public static String join(AbstractCollection<String> s) {
        return join(s, "");
    }

    /**
     * Join an Array of Strings
     *
     * @param s : String []
     * @param delimiter : String
     * @return joined String
     */
    public static String join(String s[], String delimiter) {
        if (s == null || s.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder(s[0]);
        for (int i = 1; i < s.length; ++i) {
            sb.append(delimiter).append(s[i]);
        }
        return sb.toString();
    }

    /**
     * Join an Array of Strings
     *
     * @param s : String []
     * @return joined String
     */
    public static String join(String s[]) {
        return join(s, "");
    }
}

Related

  1. getNeoString(String columnName, String delimiter, Collection input, String join)
  2. implode(Collection items, String join)
  3. internalJoin(String sep, Collection pieces)
  4. isDisjoint(Collection a, Collection b)
  5. join(CharSequence p, Collection collection)
  6. join(CharSequence separator, Collection col)
  7. join(Collection c)
  8. join(Collection c, String d)

  9. HOME | Copyright © www.java2s.com 2016