join array By Line Separator - Java java.lang

Java examples for java.lang:String Join

Description

join array By Line Separator

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String[] text = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        System.out.println(joinByLineSeparator(text));
    }/*w w  w .j a  v a  2s.  co  m*/

    private static final String separator = "\n";

    public static String joinByLineSeparator(String[] text) {
        if (text.length == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        sb.append(text[0]);
        for (int i = 1; i < text.length; ++i) {
            String txt = text[i];
            if (!txt.isEmpty()) {
                sb.append(separator).append(txt);
            }
        }
        return sb.toString();
    }
}

Related Tutorials