1

Possible Duplicate:
Assigning an array to an ArrayList in Java
java: how to convert this string to ArrayList?
How to convert a String into an ArrayList?

I have this String :

["word1","word2","word3","word4"]

The above text is not an array, but a string returned from server via GCM (Google Cloud Messaging) communication. More specific, inside a GCM class i have this:

protected void onMessage(Context context, Intent intent) {

String message = intent.getExtras().getString("cabmate");

   }

The value of the String message is ["word1","word2","word3","word4"]

Is there a way to convert it in List or ArrayList in Java?

1

3 Answers 3

3
Arrays.asList(String[])

returns a List<String>.

Sign up to request clarification or add additional context in comments.

1 Comment

Wrong; the input is a single string.
1

Something like this:

/*
@invariant The "Word" fields cannot have commas in thier values or the conversion
to a list will cause bad field breaks. CSV data sucks...
*/
public List<String> stringFormatedToStringList(String s) {
  // oneliner for the win:
  return Arrays.asList(s.substring(1,s.length()-1).replaceAll("\"","").split(","));
  // .substring  removes the first an last characters from the string ('[' & ']')
  // .replaceAll removes all quotation marks from the string (replaces with empty string)
  // .split brakes the string into a string array on commas (omitting the commas)
  // Arrays.asList converts the array to a List
}

6 Comments

s/be s.length-1; substring is start/end, not start/length. And the end index is exclusive.
This will break horribly if any of the values has a comma within it.
I know it is start/end indecies; I was pondering -2 or -1. I have fixed the indexing. As for commas in the "words", that is the problem with CSV data, commas mess it up badly. The input data being in CSV format is beyond my control. I just assume there are no commas in the "word" fields.
s/be s.length() not s.length.
thank you very much, and thank for explaining what the code means, worked perfectly..
|
1
String wordString = "[\"word1\", \"word2\", \"word3\", \"word4\"]";
String[] words = wordString.substring(1, wordString.length() - 2).replaceAll("\"", "").split(", ");
List<String> wordList = new ArrayList<>();
Collections.addAll(wordList, words);

This will do what you want. Do note that I purposely split on ", " to remove white space, it may be more prudent to call .trim() on each string in a for-each loop and then add to the List.

4 Comments

Wrong; the input is a single string.
@SoftwareMonkey My answer, and all others you commented on were posted before the OP edited with clarification that it is a string, editing mine now.
The delimiter ", " does not exist in the example String provided by the OP. The delimiter is a single "," character. Also you sort of copied my answer...
new ArrayList( Arrays.asList( new String[]{"Apple", "Mango"} ) ); use the above line . it will return ArrayList

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.