I am trying to convert a string which is a in the format
String x= "["a","1"]" ;
to
String [] vals =["a" , "1"];
I am currently doing it in two steps
- removing the [ & ] from the string
- Splitting the string with at the comma character
String x2=""a","1""
x2.split(",")
Advertisement
Answer
I exactly, don’t gather whether you want an array of Strings or an array of Integer as your result, but for the example, you have provided, this should work.
import java.util.Arrays; class HelloWorld { public static void main(String[] args) { String x = "["a","1"]"; String[] array = x.split("""); array = Arrays.stream(array).filter(i -> i.matches("[\dA-Za-z]")).toArray(String[]::new); System.out.println(Arrays.toString(array)); } }
I have filtered the array because initially there will be some empty strings and opening and closing braces as strings.