Skip to content Skip to sidebar Skip to footer

Android Convert String To String[]

I have a String ['first','second','third'] And I need to convert it to a String[] so I can loop through it. I've seen people suggest String[] mArray = {mString}; But that doesn't

Solution 1:

use this code

JSONArray temp = newJSONArray(mString);
String[] mArray = temp.join(",").split(",");

Solution 2:

You can make use of split method in String class.

If you wanna do that

1) remove all "
2) take substring inorder to remove the [ and ]
3) Then make use of split method.

Sample Code

String tmp="[\"first\",\"second\",\"third\"]".replace("\"", "");String tm[]=tmp.substring(1, tmp.length()-1).split(",");for(int i=0;i<tm.length;i++)System.out.println(tm[i]);

Post a Comment for "Android Convert String To String[]"