Java String Split by Line splitLines(String text)

Here you can find the source of splitLines(String text)

Description

split Lines

License

Open Source License

Declaration

public static List<String> splitLines(String text) 

Method Source Code


//package com.java2s;
/*//ww w.  j av  a  2 s . c o  m
 * (c) Copyright 2007-2012 by Volker Bergmann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, is permitted under the terms of the
 * GNU General Public License.
 *
 * For redistributing this software or a derivative work under a license other
 * than the GPL-compatible Free Software License as defined by the Free
 * Software Foundation or approved by OSI, you must first obtain a commercial
 * license to this software product from Volker Bergmann.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

import java.util.*;

public class Main {
    public static List<String> splitLines(String text) {
        if (text == null)
            return null;
        List<String> lines = new ArrayList<String>();
        int TEXT = 0;
        int CR = 1;
        int LF = 2;
        int mode = TEXT;
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == '\r') {
                lines.add(builder.toString());
                builder.delete(0, builder.length());
                mode = CR;
            } else if (c == '\n') {
                if (mode != CR) {
                    // only add a line if it is a standalone LF (no CRLF) 
                    lines.add(builder.toString());
                    builder.delete(0, builder.length());
                }
                mode = LF;
            } else {
                mode = TEXT;
                builder.append(c);
            }
        }
        if (text.length() == 0 || mode != TEXT || builder.length() > 0)
            lines.add(builder.toString());
        return lines;
    }
}

Related

  1. splitLines(String s)
  2. splitLines(String self)
  3. splitLines(String str, int width)
  4. splitlines(String text)
  5. splitLines(String text)
  6. splitLogLine(final String logLine)
  7. splitMultiline(String text, boolean trimLines)
  8. splitNewLines(final String input)
  9. splitNextWord(String line)