1

When I have a string like \n\n\n, and I split by \\n, I get 0. Why is this?

public class Test {

     public static void main(String []args){
        String str = "\n\n\n";
        String[] lines = str.split("\\n");
        System.out.println(lines.length);
     }
}

You can copy & paste the code into CompileOnline.

Jiew Meng
  • 84,767
  • 185
  • 495
  • 805
  • possible duplicate of [Java split() method strips empty strings at the end?](http://stackoverflow.com/questions/545957/java-split-method-strips-empty-strings-at-the-end) – Roman Feb 24 '14 at 03:59

3 Answers3

5

The token that you split on is not part of the result. Since there is nothing else, there is no item to put in the array.

This is different when you add another character to your base string though. When you do that, it will include the empty entries after all.

This can be explained by looking at the source code in java.lang.String:2305.

Consider the following excerpt:

// Construct result
int resultSize = list.size();
if (limit == 0)
     while (resultSize > 0 && list.get(resultSize - 1).length() == 0)
           resultSize--;
String[] result = new String[resultSize];
return list.subList(0, resultSize).toArray(result);

If you have 3 empty entries as in your case, resultSize will count down to 0 and essentially return an empty array.

If you have 3 empty entries and one filled one (with the random character you added to the end), resultSize will not move from 4 and thus you will get an array of 4 items where the first 3 are empty.

Basically it will remove all the trailing empty values.

String str = "\n\n\n";      // Returns length 0
String str = "\n\n\nb";     // Returns length 4
String str = "\n\n\nb\n\n"; // Returns length 4
Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
4

As said in the String javadoc:

This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

So, when you split() a String made entirely of delimiters (whatever the delimiter is), you will get only empty Strings, the delimiter not being included in the result, and, thus, they will all be considered as trailing empty strings, and not be included in the resulting array.

If you want to get everything, including the empty strings, you have two choices:

  • add something that is not a delimiter at the end of the String:

    String str = "\n\n\ne";
    String[] lines = str.split("\\n");
    System.out.println(lines.length); // prints "4"
    
  • use the two-argument split method with a negative limit:

    String str = "\n\n\n";
    String[] lines = str.split("\\n", -1);
    System.out.println(lines.length); // prints "4"
    
Florent Bayle
  • 11,520
  • 4
  • 34
  • 47
0

Because your string contains just \n

str.split(""\n") get the string after \n which is equivalent to NULL before it's next split search. Therefore you obtain 0 as the lines[] is storing NULL.

Sky
  • 3,350
  • 2
  • 14
  • 12