Search code examples
javaspecial-characterstokenizestringtokenizer

How to use special characters & | and ⊕ in the Split() method?


I tried

String text = "1&2⊕3|4";
String[] s = text.split("|⊕&");

And nothing happened, I also tried

String text = "1&2⊕3|4";
String[] s = text.split("\\|\\⊕\\&");

And nothing happened. So, what should I do?


Solution

  • The easiest way is to create a character class by adding brackets:

    String text = "1&2⊕3|4";
    String[] s = text.split("[|⊕&]");
    

    You can read more about character classes in this excellent tutorial.