Java Collection Concatenate concat(Collection a, int offset, String sep, String start, String end)

Here you can find the source of concat(Collection a, int offset, String sep, String start, String end)

Description

concat

License

Open Source License

Declaration

public static <T> String concat(Collection<T> a, int offset,
            String sep, String start, String end) 

Method Source Code

//package com.java2s;
/*/*from w ww .ja  v  a2  s  .c o  m*/
 * Copyright (C) 2011 Tatsuhiro Tsujikawa
 *
 * 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.Collection;
import java.util.Iterator;

public class Main {
    public static <T> String concat(T a[], int offset, String sep,
            String start, String end) {
        StringBuilder sb = new StringBuilder();
        sb.append(start);
        if (a.length - offset > 0) {
            sb.append(a[offset]);
            for (int i = offset + 1, len = a.length; i < len; ++i) {
                sb.append(sep).append(a[i]);
            }
        }
        sb.append(end);
        return sb.toString();
    }

    public static <T> String concat(T a[], int offset, String sep) {
        return concat(a, offset, sep, "", "");
    }

    public static <T> String concat(Collection<T> a, int offset,
            String sep, String start, String end) {
        StringBuilder sb = new StringBuilder();
        sb.append(start);
        Iterator<T> it;
        for (it = a.iterator(); offset > 0 && it.hasNext(); --offset, it
                .next())
            ;
        if (offset == 0 && it.hasNext()) {
            sb.append(it.next());
            while (it.hasNext()) {
                sb.append(sep).append(it.next());
            }
        }
        sb.append(end);
        return sb.toString();
    }

    public static <T> String concat(Collection<T> a, int offset, String sep) {
        return concat(a, offset, sep, "", "");
    }
}

Related

  1. concat(Collection c)
  2. concat(Collection items)
  3. concat(Collection parts, int start, String defaultText)
  4. concat(Collection pieces, String delim)
  5. concat(Collection a, Collection b)
  6. concat(Collection first, Collection second)
  7. concat(Collection... collections)
  8. concat(final Collection... collections)
  9. concat(final Collection... collections)