I cannot get the String split method to work properly

Bob the Bob :

I am trying to split a String from an array into multiple Strings, before and after the "^". I used the String split method, but it is just storing the whole String in the first value of the array.

Output: b[0]= x^2 Expected: b[0] = x , b[1] = 2

Here is the code:

public class test {
    public static void main(String[] args) {   
        String a[] = {"x^2"};
        String b[] = a[0].split("^");

        System.out.println(b[0]);
    }
}
Elliott Frisch :

Caret ^ is a special character in a regular expression meaning beginning of the String, escape it with \. Like,

String a[] = {"x^2"};
String b[] = a[0].split("\\^");

System.out.println(b[0]);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=142562&siteId=1