implode String - Java java.lang

Java examples for java.lang:String Join

Description

implode String

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String[] strings = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        String seperator = "-";
        System.out.println(implodeString(strings, seperator));
    }// w w w .j  ava2s  . co  m

    public static String implodeString(String[] strings, String seperator) {
        if (strings.length == 0) {
            return "";
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append(strings[0]);
            for (int i = 1; i < strings.length; i++) {
                sb.append(seperator);
                sb.append(strings[i]);
            }
            return sb.toString();
        }
    }
}

Related Tutorials