Empty result when string splited using pattern

Valentyn Hruzytskyi :

I have the follow code:

String generalRequest = "4+6*12/3";
String[] operatorsLine = generalRequest.split("[0-9]+");

In result, I have the surplus empty value in operatorsLine:

"", "+", "*", "/"

Hovewer I desire an outcome:

"+", "*", "/"


How to skip adding "" value into array, using String.split() method?

Andronicus :

You can use something like:

String[] operatorsLine = Arrays.stream(generalRequest.split("[0-9]+"))
                               .filter(s -> !s.isEmpty())
                               .toArray(String[]::new);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=85311&siteId=1
Recommended