简体   繁体   中英

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..??

  1. Remove brackets

  2. String.split() using , separator

  3. For each item remove quotes ( ' )

  4. No 4th point

You could do something like

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM