Java String Split by Line splitNewLines(final String input)

Here you can find the source of splitNewLines(final String input)

Description

split New Lines

License

Open Source License

Declaration

public static String[] splitNewLines(final String input) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002, 2010 Innoopract Informationssysteme GmbH.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  w  w .jav a 2s.c o  m
 *     Innoopract Informationssysteme GmbH - initial API and implementation
 *     EclipseSource - ongoing development
 ******************************************************************************/

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static String[] splitNewLines(final String input) {
        int length = input.length();
        List resultList = new ArrayList();
        int start = 0;
        char last = 0;
        for (int i = 0; i < length; i++) {
            char ch = input.charAt(i);
            if (ch == '\n') {
                if (last != '\r') {
                    resultList.add(input.substring(start, i));
                }
                start = i + 1;
            } else if (ch == '\r') {
                resultList.add(input.substring(start, i));
                start = i + 1;
            }
            last = ch;
        }
        resultList.add(input.substring(start, length));
        String[] result = new String[resultList.size()];
        resultList.toArray(result);
        return result;
    }
}

Related

  1. splitlines(String text)
  2. splitLines(String text)
  3. splitLines(String text)
  4. splitLogLine(final String logLine)
  5. splitMultiline(String text, boolean trimLines)
  6. splitNextWord(String line)
  7. splitString(String line)
  8. splitStringPerWord(String string, int wordsPerLine)
  9. splitter(String line, char delimeter)