简体   繁体   English

如何在java ..中将字符串列表转换为列表对象?

[英]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 使用,分隔符的String.split()

  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 . s上使用split()方法。 So, s.split(","); 因此, s.split(","); will produce an String array of the following form: ["['a']", "'b'", "'c']"] . 将产生以下形式的String数组: ["['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. 我将留给您阅读Javadoc,以弄清楚如何在数组中准确获取所需内容。

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. 编辑:写了错误的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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