I need to split an expression like
a+b-c*d/e
and get a, b, c, d, e sepe
Besides the split approach, you could also use java.util.StringTokenizer:
String myString = "a+b-c*d/e";
List operatorList = new ArrayList();
List operandList = new ArrayList();
StringTokenizer st = new StringTokenizer(myString, "+-*/", true);
while (st.hasMoreTokens()) {
String token = st.nextToken();
if ("+-/*".contains(token)) {
operatorList.add(token);
} else {
operandList.add(token);
}
}
System.out.println("Operators:" + operatorList);
System.out.println("Operands:" + operandList);
Result:
Operators:[+, -, *, /]
Operands:[a, b, c, d, e]