Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
以字符串-2x^2+3x^1+6为例,如何提取-2,3并6从这个等式中存储到字符串中?
-2x^2+3x^1+6
-2
3
6
没有给出确切的答案,但有一些提示:
使用替换方法:
全部替换-为+-.
-
+-
使用拆分方法:
// after replace effect String str = "+-2x^2+3x^1+6" String[] arr = str.split("+"); // arr will contain: {-2x^2, 3x^1, 6}
现在,每个索引值都可以单独拆分:
String str2 = arr[0]; // str2 = -2x^2; // split with x and get vale at index 0
String polynomial= "-2x^2+3x^1+6"; String[] parts = polynomial.split("x\\^\\d+\\+?"); for (String part : parts) { System.out.println(part); }
这应该有效。样本输出
polynomial= "-2x^2+3x^1+6" Output: -2 3 6 polynomial = "-30x^6+20x^3+3" Output: -30 20 3