split string By Line Separator - Java java.lang

Java examples for java.lang:String Split

Description

split string By Line Separator

Demo Code

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

public class Main {
    public static void main(String[] argv) {
        String text = "book\n2s.com";
        System.out.println(java.util.Arrays
                .toString(splitByLineSeparator(text)));
    }//from ww w.j  a  v  a 2  s .c o m

    private static final String separator = "\n";

    public static String[] splitByLineSeparator(String text) {
        ArrayList<String> splitted = new ArrayList<String>();
        for (String str : text.split(separator)) {
            if (!str.isEmpty()) {
                splitted.add(str);
            }
        }
        return splitted.toArray(new String[splitted.size()]);
    }
}

Related Tutorials