Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android converting string to array string [closed]

I have a string like this:

["477","com.dummybilling","android.test.purchased","inapp:com.dummybilling:android.test.purchased","779"]

How to have a String[] with these 5 element? Does anyone know a regex for .split() method?

Thank you very much, regular expressions make me crazy! :(

like image 893
Tenaciousd93 Avatar asked Sep 13 '13 09:09

Tenaciousd93


People also ask

What is Chararray?

A character array is a sequence of characters, just as a numeric array is a sequence of numbers. A typical use is to store a short piece of text as a row of characters in a character vector.

Which of the following method is used to convert a string into an array?

String class split(String regex) can be used to convert String to array in java. If you are working with java regular expression, you can also use Pattern class split(String regex) method.


1 Answers

Process it as JSON. Two immediate benifits would be that it would take care of any embedded commas in your data automatically and the other that you would get a String[] with unquoted strings.

String input = "[\"477\",\"com.dummybilling\",\"android.test.purchased\",\"inapp:com.dummybilling:android.test.purchased\",\"779\"]";

JSONArray jsonArray = new JSONArray(input);
String[] strArr = new String[jsonArray.length()];

for (int i = 0; i < jsonArray.length(); i++) {
    strArr[i] = jsonArray.getString(i);
}

System.out.println(Arrays.toString(strArr));

Output :

[477, com.dummybilling, android.test.purchased, inapp:com.dummybilling:android.test.purchased, 779]
like image 112
Ravi K Thapliyal Avatar answered Sep 21 '22 14:09

Ravi K Thapliyal