Amro elaswar Amro elaswar - 7 days ago 5
Java Question

change a a list inside a string to a java String[]

I have a string containing a list and by that I mean :

String list = "[\"hello\",\"everyone\"]";


How can I transform that so I can read it as a
String[]
? My question might not be very clear but please understand English is not my native language thanks everyone.

Answer

You can do it using replace and split methods of String class, e.g.:

String s = "[\"hello\",\"everyone\"]";
String[] split = s.replace("[", "").replace("]", "").split(",");
System.out.println(Arrays.toString(split));
Comments