Java Iterator Join join(Iterator objects, String separator)

Here you can find the source of join(Iterator objects, String separator)

Description

join

License

Open Source License

Declaration

public static String join(Iterator objects, String separator) 

Method Source Code

//package com.java2s;
/*//from   w  w  w  . ja  v a2s.c  o m
 * StringHelper.java   2004-3-5
 *
 * ??????: ???????(FTO)???? 2000-2005, ?????????.
 * ????????????(FTO)????????????????????????????????????????
 *
 * Copyright 2000-2005 FTO Software Team, Inc. All Rights Reserved.
 * This software is the proprietary information of FTO Software Team, Inc.
 * Use is subject to license terms.
 *
 * FTO???http://www.free-think.org
 */

import java.util.Iterator;

public class Main {

    public static String join(String[] strs, String separator) {
        int length = strs.length;
        if (length == 0)
            return "";
        StringBuffer buf = new StringBuffer(length * strs[0].length()).append(strs[0]);
        for (int i = 1; i < length; i++) {
            buf.append(separator).append(strs[i]);
        }
        return buf.toString();
    }

    public static String join(String[] strs, String separator, String additive) {
        int length = strs.length;
        if (length == 0)
            return "";
        StringBuffer buf = new StringBuffer(length * strs[0].length() + (additive.length() * 2) * length)
                .append(additive).append(strs[0]).append(additive);
        for (int i = 1; i < length; i++) {
            buf.append(separator).append(additive).append(strs[i]).append(additive);
        }
        return buf.toString();
    }

    public static String join(Iterator objects, String separator) {
        StringBuffer buf = new StringBuffer();
        if (objects.hasNext())
            buf.append(objects.next());
        while (objects.hasNext()) {
            buf.append(separator).append(objects.next());
        }
        return buf.toString();
    }

    public static String join(Iterator objects, String separator, String additive) {
        StringBuffer buf = new StringBuffer();
        if (objects.hasNext())
            buf.append(additive).append(objects.next()).append(additive);
        while (objects.hasNext()) {
            buf.append(separator).append(additive).append(objects.next()).append(additive);
        }
        return buf.toString();
    }
}

Related

  1. join(Iterator iterator, String s)
  2. join(Iterator iterator, String separator)
  3. join(Iterator iterator, String separator)
  4. join(Iterator iterator, String separator)
  5. join(Iterator iterator)
  6. join(String delimiter, Iterator iterator)
  7. join(String seperator, Iterator objects)