将逗号分隔的字符串转换为变量列表?

时间:2016-11-08 09:10:52

标签: java string split

对于Java和参考此Question,如何将此数组中的每个值保存到单独的变量中?

如果字符串值为:

1234,AAA,30

变量将是:

var1=1234
var2=AAA
var3=30

3 个答案:

答案 0 :(得分:2)

您可以在for循环中使用它

String s = "012,345AA,89";
String[] output = s.split(",");
System.out.println(output[0]);
System.out.println(output[1]);

答案 1 :(得分:1)

使用:

String str = "1234,AAA,30";
String[] variables = str.split(",");
String first = variables[0];
String second = variables[1];
String third = variables[2];

这应该有用

答案 2 :(得分:1)

如果数组大小未修复,请尝试此操作

    String str = "1234,AAA,30";
    String[] arr = str.split(",");
    Map<Object, Object> map = IntStream.range(0, arr.length).boxed()
            .collect(Collectors.toMap(in -> "var" + (in + 1), in -> arr[in], (k, v) -> v, LinkedHashMap::new));
    System.out.println(map);

输出

{var1=1234, var2=AAA, var3=30}