Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java split() method strips empty strings at the end? [duplicate]

Tags:

java

split

People also ask

Why does split return empty string?

The natural consequence is that if the string does not contain the delimiter, a singleton array containing just the input string is returned, Second, remove all the rightmost empty strings. This is the reason ",,,". split(",") returns empty array.

Can you split an empty string?

The split() method does not change the value of the original string. If the delimiter is an empty string, the split() method will return an array of elements, one element for each character of string. If you specify an empty string for string, the split() method will return an empty string and not an array of strings.

What is split () in Java?

The method split() splits a String into multiple Strings given the delimiter that separates them. The returned object is an array which contains the split Strings. We can also pass a limit to the number of elements in the returned array.


You can specify to apply the pattern as often as possible with:

String[] de = data.split(";", -1);

See the Javadoc for the split method taking two arguments for details.


have a look at the docs, here the important quote:

[...] the array can have any length, and trailing empty strings will be discarded.

If you don't like that, have a look at Fabian's comment. When calling String.split(String), it calls String.split(String, 0) and that discards trailing empty strings (as the docs say it), when calling String.split(String, n) with n < 0 it won't discard anything.