StringUtil { public static List<Line> splitLines(String s) { final List<Line> lines = new ArrayList<Line>(); int start = 0; int index = 0; while (index < s.length()) { final char ch = s.charAt(index); if (ch == Line.LF || ch == Line.CR) { final int lineEndingStart = index; index++; if (index < s.length()) { final char ch2 = s.charAt(index); if (ch2 != ch && (ch2 == Line.LF || ch2 == Line.CR)) { index++; } } final Line line = new SingleCharSequenceLine(s, start, lineEndingStart - start, index - lineEndingStart); lines.add(line); start = index; } else { index++; } } if (start < s.length()) { final Line line = new SingleCharSequenceLine(s, start, s.length() - start, 0); lines.add(line); } return lines; } private  StringUtil(); static String fixSizeString(int size, boolean alignLeft,
			char filler, long value); static String fixSizeString(int size, boolean alignLeft,
			String s); static String fixSizeString(int size, boolean alignLeft,
			char filler, String s); static List<Line> splitLines(String s); static int findStartTrimWhitespace(CharSequence s); static int findStartTrimWhitespace(CharSequence s, int start); static int findEndTrimWhitespace(CharSequence s); static int findStartTrimNewlineChars(CharSequence s); static int findStartTrimNewlineChars(CharSequence s, int start); static int findEndTrimNewlineChars(CharSequence s); static int findWhitespace(CharSequence s); static int findWhitespace(CharSequence s, int start); static boolean containsIgnoreCase(String source, String target); static boolean startsWithIgnoreCase(String s, String prefix); static int indexOfIgnoreCase(String source, String target); static boolean equalsIgnoreCase(char ch1, char ch2);  }
@Test public void testSplitLines() { final String[] input = { "hello\nworld", "hello\r\nworld", "hello\n\r\r\nworld", "hello\n\r\r\nworld\n", "hello\n\r\r\nworld\n\n", }; final Line[][] expected = { {new SimpleLine("hello", "\n"), new SimpleLine("world", "")}, {new SimpleLine("hello", "\r\n"), new SimpleLine("world", "")}, {new SimpleLine("hello", "\n\r"), new SimpleLine("", "\r\n"), new SimpleLine("world", "")}, {new SimpleLine("hello", "\n\r"), new SimpleLine("", "\r\n"), new SimpleLine("world", "\n")}, {new SimpleLine("hello", "\n\r"), new SimpleLine("", "\r\n"), new SimpleLine("world", "\n"), new SimpleLine("", "\n")}, }; for (int i = 0; i < input.length; i++) { final List<Line> actual = StringUtil.splitLines(input[i]); Assert.assertEquals(Arrays.asList(expected[i]), actual); } }