How to split a comma-separated string?
You could do this:
String str = "...";List<String> elephantList = Arrays.asList(str.split(","));
Basically the .split()
method will split the string according to (in this case) delimiter you are passing and will return an array of strings.
However, you seem to be after a List of Strings rather than an array, so the array must be turned into a list by using the Arrays.asList()
utility. Just as an FYI you could also do something like so:
String str = "...";ArrayList<String> elephantList = new ArrayList<>(Arrays.asList(str.split(","));
But it is usually better practice to program to an interface rather than to an actual concrete implementation, so I would recommend the 1st option.
You can split it and make an array then access like array
String names = "prappo,prince";String[] namesList = names.split(",");
you can access like
String name1 = namesList [0];String name2 = namesList [1];
or using loop
for(String name : namesList){System.out.println(name);}
hope it will help you .