Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27652

The right way to split a string into zero or more words

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;
}

Upvotes: 0

Views: 377

Answers (4)

Emmanuel Bourg
Emmanuel Bourg

Reputation: 11048

I suggest using the StrTokenizer from Commons Lang. It's as simple as:

return StrTokenizer(arguments).getTokentArray();

http://commons.apache.org/lang/api-release/org/apache/commons/lang3/text/StrTokenizer.html

Upvotes: 0

allingeek
allingeek

Reputation: 1388

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.

String[] split_arguments(String arguments) {
    if(arguments == null) {
        return null;
    }
    String trimmedArguments = arguments.trim();
    if(trimmedArguments.length() == 0) {
        return null;
    }

    return trimmedArguments.split("\\s+");
}

Upvotes: 4

Pieter
Pieter

Reputation: 3399

You could use StringUtils.IsBlank from apache commons to check the string before splitting. Either way, you have to do a check, but doing a check before splitting might be more logical.

Upvotes: 0

npinti
npinti

Reputation: 52185

You could use the StringUtils from Apache Commons. You can then either use one of the split methods they provide or before make a check that the string is not empty using the isNotEmpty() or isNotBlank() methods.

Upvotes: 2

Related Questions