split string in char array To Lines - Android java.lang

Android examples for java.lang:String Split

Description

split string in char array To Lines

Demo Code

import java.util.ArrayList;

public class Main{
    public final static char RCHAR = '\r';
    public final static char NCHAR = '\n';
    public static ArrayList<String> splitToLines(char[] content) {
        ArrayList<String> lines = new ArrayList<String>();
        int len = content.length;

        StringBuffer line = new StringBuffer();
        for (int i = 0; i < len; i++) {
            char c = content[i];
            line.append(c);/*from   w  w w . j a  va 2s.co  m*/
            if (c == RCHAR) {
                char nc = content[i + 1];
                if (nc == NCHAR) {
                    line.append(nc);
                    i++;
                }
                lines.add(line.toString());
                line.delete(0, line.length());
            } else if (c == NCHAR) {
                lines.add(line.toString());
                line.delete(0, line.length());
            } else {
                if (i == len - 1) {
                    lines.add(line.toString());
                }
            }
        }
        return lines;
    }

}

Related Tutorials