join an array to String from offset index - Java Collection Framework

Java examples for Collection Framework:Array Join

Description

join an array to String from offset index

Demo Code


//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        String[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        String joint = "java2s.com";
        System.out.println(join(array, joint));
    }// www .java 2 s . c o m

    public static String join(String[] array, int startIndex, String joint) {
        String result = "";
        for (int i = startIndex; i < array.length; i++) {
            result += array[i];
            if (i < array.length - 1) {
                result += joint;
            }
        }
        return result;
    }

    public static String join(String[] array, String joint) {
        return join(array, 0, joint);
    }

    public static String join(Collection<?> collection, String joint) {
        String result = "";
        int i = 0;
        int size = collection.size();
        for (Object each : collection) {
            result += each.toString();
            if (i < size - 1) {
                result += joint;
            }
        }
        return result;
    }
}

Related Tutorials