Java Iterable Join join(Iterable s, String delimiter)

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

Description

join

License

LGPL

Declaration

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

Method Source Code


//package com.java2s;
/*/*w  w w .  j av  a  2  s  .  c o  m*/
 * ============================================================================
 * GNU Lesser General Public License
 * ============================================================================
 *
 * ZKTest - Free ZeroKode testing library.
 * Copyright (C) 2011 Telesoft Consulting GmbH http://www.telesoft-consulting.at
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 * 
 * 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; If not, see http://www.gnu.org/licenses/
 * 
 * Telesoft Consulting GmbH
 * Gumpendorferstra?e 83-85
 * House 1, 1st Floor, Office No.1
 * 1060 Vienna, Austria
 * http://www.telesoft-consulting.at/
 */

import java.util.Iterator;

public class Main {
    public static String join(Object[] s, String delimiter) {
        if (s == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder(64);
        for (int i = 0; i < s.length; i++) {
            if (s[i] != null) {
                sb.append(s[i].toString());
            }
            if (i < s.length - 1) {
                sb.append(delimiter);
            }
        }
        return sb.toString();
    }

    public static String join(Iterable<String> s, String delimiter) {
        StringBuilder buffer = new StringBuilder(64);
        Iterator iter = s.iterator();
        if (iter.hasNext()) {
            buffer.append(nvl(iter.next(), ""));
            while (iter.hasNext()) {
                buffer.append(delimiter);
                buffer.append(nvl(iter.next(), ""));
            }
        }
        return buffer.toString();
    }

    public static String nvl(String value) {
        return nvl(value, "".intern());
    }

    public static String nvl(String value, String replacement) {
        if (value == null) {
            return replacement;
        }

        return value;
    }

    public static String nvl(Object value, String replacement) {
        if (value == null) {
            return replacement;
        }

        return value.toString();
    }

    public static <T extends Number> T nvl(T value, T replacement) {
        if (value == null) {
            return replacement;
        }

        return value;
    }
}

Related

  1. join(Iterable array)
  2. join(Iterable items, String separator)
  3. join(Iterable iterable, String delimiter)
  4. join(Iterable s, String delimiter)
  5. join(Iterable s, String delimiter)
  6. join(Iterable source, char delimiter)
  7. join(Iterable source, String separator)
  8. join(Iterable strings)
  9. join(Iterable strings, String delimiter)