Home > Mobile >  Split the String when it contains operation symbols in Java?
Split the String when it contains operation symbols in Java?

Time:11-11

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']

CodePudding user response:

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]
  • Related