将逗号分隔的ArrayList值转换为String - Java

时间:2022-01-21 00:17:12

I'm trying to get the String values as

我正在尝试将String值作为

"one","two","three", "four",

But what I currently get is

但我现在得到的是

"one","two","three, four",

Here's the code, I've a comma separated value in the ArrayList but I want to take that comma separated values as two distinct values and not as one.

这是代码,我在ArrayList中有一个逗号分隔值但是我想把逗号分隔的值作为两个不同的值而不是一个。

ArrayList<String> list = new ArrayList<String>();
list.add("one");
list.add("two");
list.add("three, four");

String listString = "";

for (String s : list)
{
    listString += "\"" + s + "\",";
}

System.out.println(listString);

Is it possible? If yes, how do I do that?

可能吗?如果是,我该怎么做?

3 个解决方案

#1


2  

Your problem is that the ArrayList won't automatically split on the comma. If you add "three, four" into an ArrayList, then a single entry with the value "three, four" will be added.

您的问题是ArrayList不会自动在逗号上拆分。如果将“three,four”添加​​到ArrayList中,则将添加值为“three,four”的单个条目。

Therefore, you'll need to split "three, four" yourself - the split method returns an array:

因此,您需要自己拆分“三,四” - split方法返回一个数组:

String[] strings = "three, four".split(", ");
list.add(strings[0]);
list.add(strings[1]);

Bear in mind that the argument to the split method is a regular expression - it won't make any difference in this case, but it can be a gotcha in certain situations.

请记住,split方法的参数是一个正则表达式 - 在这种情况下它不会有任何区别,但在某些情况下它可能是一个问题。

#2


1  

When you iterate through the list, you can check if the string contains a comma and if it does, then split by ", " (using string.split(", ")) and then print out each string with the formatting that you want.

当你遍历列表时,可以检查字符串是否包含逗号,如果是,则用“,”(使用string.split(“,”))拆分,然后打印出你想要的格式的每个字符串。

The ArrayList will not split your third entry. It just assumes it as a single string entry and hence you get the result you see.

ArrayList不会拆分您的第三个条目。它只是假设它是一个单独的字符串条目,因此你得到你看到的结果。

#3


1  

This will do it as expected:

这将按预期执行:

   list.addAll(Arrays.asList("three, four".split(",")));

#1


2  

Your problem is that the ArrayList won't automatically split on the comma. If you add "three, four" into an ArrayList, then a single entry with the value "three, four" will be added.

您的问题是ArrayList不会自动在逗号上拆分。如果将“three,four”添加​​到ArrayList中,则将添加值为“three,four”的单个条目。

Therefore, you'll need to split "three, four" yourself - the split method returns an array:

因此,您需要自己拆分“三,四” - split方法返回一个数组:

String[] strings = "three, four".split(", ");
list.add(strings[0]);
list.add(strings[1]);

Bear in mind that the argument to the split method is a regular expression - it won't make any difference in this case, but it can be a gotcha in certain situations.

请记住,split方法的参数是一个正则表达式 - 在这种情况下它不会有任何区别,但在某些情况下它可能是一个问题。

#2


1  

When you iterate through the list, you can check if the string contains a comma and if it does, then split by ", " (using string.split(", ")) and then print out each string with the formatting that you want.

当你遍历列表时,可以检查字符串是否包含逗号,如果是,则用“,”(使用string.split(“,”))拆分,然后打印出你想要的格式的每个字符串。

The ArrayList will not split your third entry. It just assumes it as a single string entry and hence you get the result you see.

ArrayList不会拆分您的第三个条目。它只是假设它是一个单独的字符串条目,因此你得到你看到的结果。

#3


1  

This will do it as expected:

这将按预期执行:

   list.addAll(Arrays.asList("three, four".split(",")));