I have a String looking like this: ["a","b","c","d"]
. Notice that this is a String
and not a String array. However I want to convert this to a List<String>
. How can I achieve this the easiest way?
Asked
Active
Viewed 93 times
-1

moviaa
- 402
- 1
- 4
- 16
-
2it would really nice if you would share the solution that you are working on. – Olimpiu POP Oct 03 '14 at 21:29
-
1@moviaa if it's a String than perhaps is: "[a,b,c,d]" ? – Alboz Oct 03 '14 at 21:31
-
try to do a split and some string manipulation – pedrommuller Oct 03 '14 at 21:32
-
2What do you want in your List? What is the actual String you are parsing? `["a","b", "c", "d"]` is four Strings, in what looks like a JSON array format. Please be more specific with your exact input and desired output. – azurefrog Oct 03 '14 at 21:37
-
@jack.the.ripper: I'm trying to do a split, but not sure how the regexp should look like. I have tried a few, but can't get it to work. – moviaa Oct 03 '14 at 21:38
-
@azurefrog: At the bottom it is actually a JSON array. However, I'm working with an Android application where I'm sending a GCM message with this JSON object. In the applications's IntentService I have this message (JSON) in a Bundle. When I try to retrieve this JSON object as bundle.getStringArray(key) I can't retrieve anything. However bundle.getString(key) works and I get the above shown String object. – moviaa Oct 03 '14 at 21:44
-
@azurefrog it seems pretty clear to me. What OP gave was a single string, starting with a `[`, then a `"`, and so on. He wants the strings that are encapsulated within that string to be placed into a list. – chiastic-security Oct 03 '14 at 21:46
-
2@moviaa If it's actually legal JSON, why not just use a JSON parser? – azurefrog Oct 03 '14 at 21:47
-
@chiastic-security It's not obvious to me, which is why I asked. There's a rather large difference in my mind between `list.add("a");` and `list.add("\"a\"");` for instance. Without clarification from the OP, I cannot guess exactly what the input is or how it ought to be parsed. – azurefrog Oct 03 '14 at 21:53
-
@azurefrog: You're right - I was getting too blinded in solving it by parsing myself. I'm now retrieving the JSON as a String object from the Bundle and then parsing it to a JSON-object and then it's quite easy to get a List from there. Thanks! – moviaa Oct 03 '14 at 21:55
3 Answers
0
Maybe this way?
String s = "[\"a\",\"b\", \"c\", \"d\"]";
List<String> list = Arrays.asList(s.substring(1, s.length() - 1).replaceAll("[\" ]", "").split(","));
EDIT
String s = "[\"a\",\"b\", \"c\", \"d\"]";
List<String> list = Arrays.asList(s.substring(1, s.length() - 1).split(","));
for (int i = 0; i < list.size(); i++) {
String ss = list.get(i).trim();
list.set(i, ss.substring(1, ss.length() - 1));
}
EDIT #2
String s = "[\"a\",\"b\",\"c\",\"d\"]";
List<String> list = Arrays.asList(s.substring(2, s.length() - 2).split("\",[ ]*\""));

Krayo
- 2,492
- 4
- 27
- 45
-
This will go wrong if there are commas in any of the strings. – chiastic-security Oct 03 '14 at 21:37
-
In fact it'll also go wrong if there are spaces in the strings! – chiastic-security Oct 03 '14 at 21:41
0
As pointed out in the comments, this string is just a JSON array - you should just use a JSON parser rather than try to invent your own.
This other SO answer (and all the other answers on the page) provide some instructions on how to do this.
-2
This is my implementation:
List<String> list = Arrays.asList(theString.split(""));
Hope that helps. SALAM
Edit
Sorry I didn't get the question very well This is a new implementation:
List<String> list = Arrays.asList(theString.replace("\"", "").replace("[", "").replace("]", "").split(","));
list.forEach(e-> e.trim());
this not the best implementation but this is what i came with :P

BilalDja
- 1,072
- 1
- 9
- 17