I have 2 Strings.
String s1 = "x^5 + 22.6x^4 - x^3 - 41.3x^1 + 4.2";
String s2 = "-x^3 - 3x^2 + 2x + 22";
I want to split the strings. I should find coefficients and exponents.
I used replace method like this: s1.replaceAll("//s+",""); So I remove all whitespace.
When I use split method.
String array[] = s2.split("//+");
My outputs are :
-x^3-3x^2
+2x
+22
But it is not answer. Using one split method, I will divide all the parts.
But I want to split the strings when special code "+" and "-" together. But I didn't again. Without removing whitespaces, can I split my Strings?
One way is to iterate throught all the substrings of your polinome that contains no whitespace ( basically all variable and operator itself. Then you can decide if you see an operator or something else in your current iteration and handle them separately. If you don't look at an operator, you can just split your current match by the character ^, and the 0th find is your coeff, and the 1th is your exponent.