join Collection by passed in separator - Java java.util

Java examples for java.util:Collection Join

Description

join Collection by passed in separator

Demo Code

// Copyright 2012 Google Inc. All Rights Reserved.
//package com.java2s;
import java.util.Collection;

public class Main {
    public static void main(String[] argv) {
        Collection list = java.util.Arrays.asList("asdf", "java2s.com");
        String inbetween = "java2s.com";
        System.out.println(join(list, inbetween));
    }/*ww w  .j a  va  2 s. c  o m*/

    public static final String join(final Collection<?> list,
            final String inbetween) {
        final StringBuilder builder = new StringBuilder();
        for (final Object object : list) {
            if (builder.length() > 0) {
                builder.append(inbetween);
            }
            builder.append(object.toString());
        }
        return builder.toString();
    }
}

Related Tutorials