Removing empty values from string split method in Java
In this tutorial, we are going to learn about how to remove the empty values from a string.split() method in Java.
Removing the empty values
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