Take the 2-minute tour ×

in C# I can use the StringSplitOption.None enum element to specify that I want empty strings to be included in the resulting array:

String[] s = text.Split(new String[] { "\r\n" }, StringSplitOptions.None);

In java I can do the following:

 String[] s= text.split("\r\n");

However, the split method does not return empty strings. (So if text = "a\r\nb\r\n", I want to get {a, b, ""}, rather than {a, b}).

Is there an easy way to accomplish this either via a regex or using some other method?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

You could use -1 as your String#split limit:

System.out.println(Arrays.toString("a\r\nb\r\n".split("\\r\\n", -1)));
share|improve this answer
that works. Thanks :) –  John Smith Feb 12 '13 at 23:14
add comment

Use Guava's Splitter. Generally much more logical in terms of how it works.

import com.google.common.base.Splitter;
share|improve this answer
add comment

Your Answer


