Is there any string out there that could return an array with a null node when .split is called? Here’s what I tried:
String string = "test,,test";
if (string != null && !string.isEmpty()) {
String[] parsedString = string.split(",");
for (String stringNode : parsedString) {
if (stringNode != null) {
//perform logic here. Omitted for the purpose of this question.
}
}
}
I want my method to be able handle a null string, but if String.split() can’t return an array with a null node, then it is probably safe to remove right?
No, String.split(String regex) can never return an array with a
nullelement in it.There are only three cases to account for:
regexis invalid, aPatternSyntaxExceptionis thrownregexisn’t found, the array will only contain the original string.regexis found, the string is split.