Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Guavas Splitter and retain delimiters (without regex)?

Tags:

java

split

guava

Is there a simple way of using Guavas Splitter to split a string and retain the delimiters without using regex?

Something like

String string = "1+2-3*40";
Splitter splitter = Splitter.on(CharMatcher.DIGIT.negate()).retainDelimiters();

That gives

[1, +, 2, -, 3, *, 40]

I know about Splitter.onpattern() but that would require I give it a regular expression (yet thats what am trying to avoid).

like image 494
anthonyms Avatar asked Nov 12 '22 10:11

anthonyms


1 Answers

I don't think it can be done in Guava now but you may submit a feature request. BTW what would be the output for "1+3**2"? I think pattern would be unambiguous here.

like image 164
Xaerxess Avatar answered Nov 15 '22 07:11

Xaerxess