0

I want to split a string by WHITE_SPACE, LPAREN or RPAREN.

I've tried this:

String st = "( a * b - (c * d )";
String[] subs = split.("(\\s+|\\(|\\))");

This kind of works, but it gives me empty strings in subs.

I expected to get "a", "*", "-", "c", "*", "d". But I actually got "", "", "a", "*", "-", "c", "*", "d". (basically, a bunch of extra empty strings)

What did I do wrong?

3
  • Nothing wrong, since you have ( and a space at the start, you're getting empty tokens. Commented Sep 28, 2016 at 16:05
  • How do I eliminate the empty tokens? Commented Sep 28, 2016 at 16:06
  • subs = st.replaceFirst("^[()\\s]+", "").split("[()\\s]+"); Commented Sep 28, 2016 at 16:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.