2

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]); } } 

1 Answer 1

4

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]); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.