Java Array Join join(String[] ar, String sep)

Here you can find the source of join(String[] ar, String sep)

Description

join

License

Open Source License

Declaration

public static String join(String[] ar, String sep) 

Method Source Code

//package com.java2s;
/*/*  w  ww.  j  av a  2s  .co m*/
 * (C) Copyright 2006-2010 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     bstefanescu
 */

import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class Main {
    public static String join(String[] ar, String sep) {
        if (ar == null || ar.length == 0) {
            return null;
        }
        if (ar.length == 1) {
            return ar[0];
        }
        StringBuilder buf = new StringBuilder();
        buf.append(ar[0]);
        for (int i = 1; i < ar.length; i++) {
            buf.append(sep).append(ar[i]);
        }
        return buf.toString();
    }

    public static String join(String[] ar, char sep) {
        if (ar == null || ar.length == 0) {
            return null;
        }
        if (ar.length == 1) {
            return ar[0];
        }
        StringBuilder buf = new StringBuilder();
        buf.append(ar[0]);
        for (int i = 1; i < ar.length; i++) {
            buf.append(sep).append(ar[i]);
        }
        return buf.toString();
    }

    public static String join(List<String> list, String sep) {
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() == 1) {
            return list.get(0);
        }
        StringBuilder buf = new StringBuilder();
        buf.append(list.get(0));
        for (int i = 1, len = list.size(); i < len; i++) {
            buf.append(sep).append(list.get(i));
        }
        return buf.toString();
    }

    public static String join(List<String> list, char sep) {
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() == 1) {
            return list.get(0);
        }
        StringBuilder buf = new StringBuilder();
        buf.append(list.get(0));
        for (int i = 1, len = list.size(); i < len; i++) {
            buf.append(sep).append(list.get(i));
        }
        return buf.toString();
    }

    public static String join(Set<String> list, String sep) {
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() == 1) {
            return list.iterator().next();
        }
        StringBuilder buf = new StringBuilder();
        Iterator<String> it = list.iterator();
        buf.append(it.next());
        while (it.hasNext()) {
            buf.append(sep).append(it.next());
        }
        return buf.toString();
    }

    public static String join(Set<String> list, char sep) {
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() == 1) {
            return list.iterator().next();
        }
        StringBuilder buf = new StringBuilder();
        Iterator<String> it = list.iterator();
        buf.append(it.next());
        while (it.hasNext()) {
            buf.append(sep).append(it.next());
        }
        return buf.toString();
    }
}

Related

  1. join(String separator, String[] strings)
  2. join(String separator, String[] strings)
  3. join(String[] a, String delimiter, Integer startIndex)
  4. join(String[] a, String[] b)
  5. join(String[] a1, String[] a2)
  6. join(String[] args)
  7. join(String[] args, String delimiter)
  8. join(String[] array)
  9. join(String[] array)