user3424455 user3424455 - 1 year ago 78
Java Question

Create ArrayList from comma separated String without whitespace

Say I have a String with randomly placed spaces and commas around other characters.

String str = "item1 , it em 2, ite m 3"

How do I get an ArrayList with the items like

[item1, item2, item3]

Answer Source

Use the following:

String str = "item1 , it em 2,  ite m 3"
String[] splitArray = str.split(",");
ArrayList<String> list = new ArrayList<String>();
for (String s:splitArray)
    list.add(s.replace(" ", ""));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download