user1252812
user1252812

Reputation:

How to convert String list into List Object in java..?

I have a String as follows :

s = "['a','b','c']"

How can i convert this string into List Object..??

Upvotes: 0

Views: 11545

Answers (3)

smessing
smessing

Reputation: 4350

Use the split() method on s. So, s.split(","); will produce an String array of the following form: ["['a']", "'b'", "'c']"]. I'll leave it to you to read the javadoc to figure out how to get exactly what you want in the array.

Once have the array, you can add all the elements to a List using the following:

List<String> list = Arrays.asList(split);

EDIT: wrote wrong method.

Upvotes: 0

AnAmuser
AnAmuser

Reputation: 1895

You could do something like

s = s.replace("[", "").replace("]", "");
String[] split = s.split(",");
List<String> list = Arrays.asList(split);

Upvotes: 5

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340693

  1. Remove brackets

  2. String.split() using , separator

  3. For each item remove quotes (')

  4. No 4th point

Upvotes: 2

Related Questions