Java String Split by Line splitLines(String S)

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

Description

Splits a String into lines on any '\n' characters.

License

Open Source License

Declaration

public static List<String> splitLines(String S) 

Method Source Code

//package com.java2s;
/*//from ww  w  .  j  av  a 2s . c o m
 * aocode-public - Reusable Java library of general tools with minimal external dependencies.
 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2018, 2019  AO Industries, Inc.
 *     support@aoindustries.com
 *     7262 Bull Pen Cir
 *     Mobile, AL 36695
 *
 * This file is part of aocode-public.
 *
 * aocode-public is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aocode-public is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with aocode-public.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Splits a String into lines on any '\n' characters.  Also removes any ending '\r' characters if present
     */
    public static List<String> splitLines(String S) {
        List<String> V = new ArrayList<>();
        int start = 0;
        int pos;
        while ((pos = S.indexOf('\n', start)) != -1) {
            String line;
            if (pos > start && S.charAt(pos - 1) == '\r')
                line = S.substring(start, pos - 1);
            else
                line = S.substring(start, pos);
            V.add(line);
            start = pos + 1;
        }
        int slen = S.length();
        if (start < slen) {
            // Ignore any trailing '\r'
            if (S.charAt(slen - 1) == '\r')
                slen--;
            String line = S.substring(start, slen);
            V.add(line);
        }
        return V;
    }

    /**
     * Finds the first occurrence of any of the supplied characters
     *
     * @param  S  the <code>String</code> to search
     * @param  chars  the characters to look for
     *
     * @return  the index of the first occurrence of <code>-1</code> if none found
     */
    public static int indexOf(String S, char[] chars) {
        return indexOf(S, chars, 0);
    }

    /**
     * Finds the first occurrence of any of the supplied characters starting at the specified index.
     *
     * @param  S  the <code>String</code> to search
     * @param  chars  the characters to look for
     * @param  start  the starting index.
     *
     * @return  the index of the first occurrence of <code>-1</code> if none found
     */
    public static int indexOf(String S, char[] chars, int start) {
        int Slen = S.length();
        int clen = chars.length;
        for (int c = start; c < Slen; c++) {
            char ch = S.charAt(c);
            for (int d = 0; d < clen; d++)
                if (ch == chars[d])
                    return c;
        }
        return -1;
    }

    /**
     * Finds the next of a substring like regular String.indexOf, but stops at a certain maximum index.
     * Like substring, will look up to the character one before toIndex.
     */
    public static int indexOf(String source, String target, int fromIndex, int toIndex) {
        if (fromIndex > toIndex)
            throw new IllegalArgumentException(
                    "fromIndex>toIndex: fromIndex=" + fromIndex + ", toIndex=" + toIndex);

        int sourceCount = source.length();

        // This line makes it different than regular String indexOf method.
        if (toIndex < sourceCount)
            sourceCount = toIndex;

        int targetCount = target.length();

        if (fromIndex >= sourceCount) {
            return (targetCount == 0 ? sourceCount : -1);
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (targetCount == 0) {
            return fromIndex;
        }

        char first = target.charAt(0);
        int max = sourceCount - targetCount;

        for (int i = fromIndex; i <= max; i++) {
            /* Look for first character. */
            if (source.charAt(i) != first) {
                while (++i <= max && source.charAt(i) != first) {
                    // Intentionally empty
                }
            }

            /* Found first character, now look at the rest of v2 */
            if (i <= max) {
                int j = i + 1;
                int end = j + targetCount - 1;
                for (int k = 1; j < end && source.charAt(j) == target.charAt(k); j++, k++) {
                    // Intentionally empty
                }

                if (j == end) {
                    /* Found whole string. */
                    return i;
                }
            }
        }
        return -1;
    }
}

Related

  1. splitLine(String input, int splitPosition)
  2. SplitLine(String p_line, String p_delimiter)
  3. splitLines(final String helpMessage)
  4. splitLines(final String string)
  5. splitLines(String raw, int limit)
  6. splitLines(String s)
  7. splitLines(String self)
  8. splitLines(String str, int width)
  9. splitlines(String text)