Here is my code:
serialNumbers = "";
String[] serialArray = serialNumbers.split(",");
int arrayLength = serialArray.length;
arrayLength is showing 1 even there have no value in serialArray. I was expecting that length should return 0 in this case.
In the case of splitting an empty string, the first mode (no argument) will return an empty list because the whitespace is eaten and there are no values to put in the result list. In contrast, the second mode (with an argument such as \n ) will produce the first empty field.
length property. If the length of the object is 0, then the array is considered to be empty and the function will return TRUE.
Using split() When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.
To split a string with comma, use the split() method in Java. str. split("[,]", 0); The following is the complete example.
From the doc:
If the expression does not match any part of the input then the resulting array has just one element, namely this string.
Note that this doc is from the String.split(String, int)
method, which is invoked from String.split(String)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With