Guava Splitter with limit() and omitEmptyStrings() is inconsistent

Viewed 110

Question: is this desired behavior, or a bug?

The Javadoc does not mention this corner case. I can understand how the code produces this result, because only once the limit size counter is reached, splitter characters are kept in the content.

String key = "mykey";
String value = "=2>@C=b";
String combined = key +"=" + value;

Splitter.on('=').limit(2).omitEmptyStrings().splitToList(combined);

Result: (the first equal sign of the value is lost)

[mykey, 2>@C=b]

Splitter.on('=').limit(2).splitToList(combined);

Result: as expected.

[mykey, =2>@C=b]

The Javadoc for omitEmptyStrings() says:

"Returns a splitter that behaves equivalently to this splitter, but automatically omits empty strings from the results."

I am using trimResults() and omitEmptyStrings() to trim off whitespace. In no way did I expect having content split off by this. When declaring a limit(2) I expect to have everything after the first splitter character to be assigned to the 2nd value.

Tested in Guava versions 29.0-jre and 21, same results.

0 Answers
Related