Is there any built-in method in Java which allows us to convert comma separated String to some container (e.g array, List or Vector)? Or do I need to write custom code for t
There is no built-in method for this but you can simply use split() method in this.
String commaSeparated = "item1 , item2 , item3";
ArrayList<String> items =
new ArrayList<String>(Arrays.asList(commaSeparated.split(",")));
This code will help,
String myStr = "item1,item2,item3";
List myList = Arrays.asList(myStr.split(","));
List<String> items= Stream.of(commaSeparated.split(","))
.map(String::trim)
.collect(toList());
An example using Collections
.
import java.util.Collections;
...
String commaSeparated = "item1 , item2 , item3";
ArrayList<String> items = new ArrayList<>();
Collections.addAll(items, commaSeparated.split("\\s*,\\s*"));
...
List commaseperated = new ArrayList();
String mylist = "item1 , item2 , item3";
mylist = Arrays.asList(myStr.trim().split(" , "));
// enter code here
List<String> items = Arrays.asList(s.split("[,\\s]+"));