0

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 work for me, do I need to format my String differently first before converting it?

1
  • I have added the sample code in my answer. If needed you can use that. Commented Sep 11, 2013 at 9:07

2 Answers 2

7

use this code

JSONArray temp = new JSONArray(mString);
String[] mArray = temp.join(",").split(",");
Sign up to request clarification or add additional context in comments.

1 Comment

I've just realized I can use a JSONArray rather than String[], I can just loop the JSONArray.
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]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.