我有以下字符串,想把它转换成字符串数组
String singleArray = "[501]"
String multipleArray = "[1,501,634]"
我想得到 List<String>
从那个,
事情是有时它可以是一个单一的值(value)
有时它可以是用逗号分隔的多个值。
我尝试使用 Arrays.asList(multipleArray)
但它没有用。
有什么建议吗?
最佳答案
- 获取第一个和最后一个字符之间的子字符串,以去除
[
和]
字符。 - 用
,
分割结果字符串。
例如:
String input = "[1,501,634]";
String[] result = input.substring(1, input.length() - 1).split(",");
List<String> asList = Arrays.asList(result);
关于Java 将字符串数组转换为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29486869/