by @codeexamples

Removing empty values from string split method in Java

Home » java » Removing empty values from string split method in Java

To remove the empty values from a string.split() method result array, we need to pass -1 as a second argument to it.

Here is an example:

String data = "1|2|3||8|9|";
String[] split = data.split("\\|", -1);
System.out.println(split.length);

Output:

7