split a polynomial string with more the one delimiter and keep the delimiters? (JAVA) -
for example if got string "1+3-2+45-6" need array of strings {1,+3,-2,+45,-6}
i tried split twice, 1 time "+" , time "-". inconvenient. there easy way in java it?
thanks in advance!
use positive lookahead based regex. (?=[+-]) regex matches boundary exists before + or - symbols. splitting input according matched boundary give desired output.
string.split("(?=[+-])"); example:
string s = "1+3-2+45-6"; string[] parts = s.split("(?=[+-])"); system.out.println(arrays.tostring(parts)); output:
[1, +3, -2, +45, -6]
Comments
Post a Comment