StringSplitter
Prefer Splitter to String.split

Severity
WARNING
Has Fix?
REQUIRES_HUMAN_ATTENTION

The problem

String.split(String) has surprising behaviour. For example, consider the following puzzler from http://konigsberg.blogspot.com/2009/11/final-thoughts-java-puzzler-splitting.html:

String[] nothing = "".split(":");
String[] bunchOfNothing = ":".split(":");

The result is [""] and []!

Prefer guava’s String.splitter, which has more predicitable behaviour and provides explicit control over the handling of empty strings and the trimming of whitespace.

Alternately, consider using String.split(String, int) and setting an explicit ‘limit’.

TIP: consider extracting the Splitter instance to a static final field.

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("StringSplitter") to the enclosing element.