Search code examples
guavasplitter

Guava splitter with limit from backwards


I would like to split string from backward and omit last two occurrences.

Example

String:

"foo:bar:baz:boo:ban"

And I would like to omit last two :

and get

foo bar baz


Solution

  • List<String> all = Splitter.on(':').splitToList("foo:bar:baz:boo:ban");
    List<String> allButLastTwo = all.subList(0, all.size() - 2);