I've gone through the String's split method documentation but the results are not as expected. When we split a string with the limit argument set to a negative value it always append an empty value. Why should it do that? Consider some cases
// Case 1
String str = "1#2#3#";
System.out.println(str.split("#").length); // Prints 3
System.out.println(str.split("#", -1).length); // Prints 4
What i would expect here is both prints 3.
// Case 2
str = "";
System.out.println(str.split("#").length); // Prints 1
System.out.println(str.split("#", -1).length); // Prints 1
Now since no match is found the usual split method without limit was supposed to print 0 but it creats an array with an empty string.
// Case 3
str = "#";
System.out.println(str.split("#").length); // Prints 0
System.out.println(str.split("#", -1).length); // Prints 2
Now i have a match and the split method without limit argument works fine. Its is my expected output but why wouldnt it create an empty array in this case as in case 2?
// Case 4
str = "###";
System.out.println(str.split("#").length); // Prints 0
System.out.println(str.split("#", -1).length); // Prints 4
Here first split method is as expected but why does the second one gives 4 instead of 3?
// Case 5
str = "1#2#3#";
System.out.println(str.split("#", 0).length); // Prints 3
System.out.println(str.split("#", 3).length); // Prints 3
System.out.println(str.split("#", 4).length); // Prints 4
Now the last case with positive limit. If the positive amount is <= the number of match the result is as expected. But if we give a higher positive limit it again appends an empty string to the resulting array.