I need to split an expression like
a+b-c*d/e
and get a, b, c, d, e seperately(as array of strings) as well as =,-,*,d,/(also an array of operators) separately. I have tried like:
String myString;
String myString={"a+b-c*d/e");
String[] result=new String();
String[] separator=new String[]{"+","-","/","*"};
result=myString.split(separator);
But, it shows an error. How to solve it?
1st problem: –
Multiple declaration of
String myString;2nd problem: –
String initialized incorrectly. Double quotes missing at the ends. Remove bracket and brace from the ends.
3rd problem: –
String array initialized with an String object, rather than an array object.
In fact, you don’t need to initialize your array before hand.
4th problem: –
String.splittakes a Regular Expression as argument, you have passed an array. Will not work.Use: –
to split on all the operators.
And regarding your this statement: –
I don’t understand what you want here. Your sample string does not contains
=. Anddis not anoperator. Please see if you want to edit it.UPDATE : –
If you mean to keep the operators as well in your array, you can use this regex: –
OUTPUT : –
(?<=...)meanslook-behind assertion, and(?=...)meanslook-ahead assertion.