0

I want to split the string when it contains the symbol "+" and "-", how can I do that?

Example:

str1 = "2x^3+3x-8";


//Result:
['2x^3', '3x', '8']
joshxb2
  • 15
  • 5

1 Answers1

1

A regex split should work here:

String str1 = "2x^3+3x-8";
String[] parts = str1.split("[+-]");
System.out.println(Arrays.toString(parts));  // [2x^3, 3x, 8]
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360