What I am trying to achieve:

I have an input string, which I would like to

  1. transform all sequences of (one or multiple tab characters) into a delimiter ";"
  2. split the transformed string by ";"
  3. do something with each elements of the splits (this step is not important, question is on step 1 and 2)

And hopefully, comply with ErrorProne while doing so.

What did I try:

    private static final Pattern MYPATTERN= Pattern.compile("\\t+");

    private String question(String input) {
        final String inputMessageWithoutTab = MYPATTERN.matcher(input).replaceAll(";");
        final String[] splitByComma = inputMessageWithoutTab.split(";");
        return splitByComma[0]; //not important
    }

With code above, which works for my usecase, I am always flagged with

[StringSplitter] String.split(String) has surprising behavior
    (see https://errorprone.info/bugpattern/StringSplitter)

Question:

I do not use Guava (and would like to stay away from it)

Therefore, the second option, the doc says:

String.split(String, int) or Pattern.split(CharSequence, int) and setting an explicit ‘limit’ to -1 to match the behaviour of Splitter.

if I do:

    private String question(String input) {
        final String inputMessageWithoutTab = MYPATTERN.matcher(input).replaceAll(";");
        final String[] splitByComma = String.split(inputMessageWithoutTab, -1); //HERE
        return splitByComma[0];
    }

The code simply does not compile.

What is the proper way to use split, which fix the ErrorProne issue?

0

There are 0 best solutions below