逗号分隔字符串中的值到向量 - Java

时间:2022-10-01 00:20:19

In java how can i convert a string with comma separating values into a vector. So when a comma is encountered in the string, that value is added to the vector

在java中如何将逗号分隔值转换为矢量的字符串。因此,当在字符串中遇到逗号时,该值将添加到向量中

2 个解决方案

#1


1  

As justRadojko suggested, you can use the String's method .split(). Turning this into an List would be trivial:

正如JustRadojko建议的那样,你可以使用String的方法.split()。将其转换为List将是微不足道的:

String[] temp = string.split(",");
for(String s : temp){
    list.add(s);
}

#2


1  

Try something like this:

尝试这样的事情:

String s = "1, 2, 3, 4";
String[] sa = s.split(",");

Vector vec = new Vector();
vec.addAll(Arrays.asList(sa));

#1


1  

As justRadojko suggested, you can use the String's method .split(). Turning this into an List would be trivial:

正如JustRadojko建议的那样,你可以使用String的方法.split()。将其转换为List将是微不足道的:

String[] temp = string.split(",");
for(String s : temp){
    list.add(s);
}

#2


1  

Try something like this:

尝试这样的事情:

String s = "1, 2, 3, 4";
String[] sa = s.split(",");

Vector vec = new Vector();
vec.addAll(Arrays.asList(sa));