I have a string with zero or more whitespace-separated words, that needs to be split into an array of words. This is what I did. But the post-processing step, where I handle the case that the input string contained no words, suggests to me that I should have used some other way than String.split(). Should I?
String[] split_arguments(String arguments) {
String[] result = arguments.split("[\t ]+");
if (result.length == 1 && result[0].equals("")) {
result = new String[0];
}
return result;
}
Why not simply trim the leading and trailing whitespace and check for that case prior to splitting. Also, you might simply use the predefined whitespace character class.