开发者

Adding comma separated strings to an ArrayList and vice versa

开发者 https://www.devze.com 2023-03-08 04:58 出处:网络
How to add a comma separated string to an ArrayList? My string \"returnedItems\" could hold 1 or 20 items which I\'d like to add to my ArrayList \"selItemArrayList\".

How to add a comma separated string to an ArrayList? My string "returnedItems" could hold 1 or 20 items which I'd like to add to my ArrayList "selItemArrayList".

After the ArrayList has been populated, I'd开发者_JS百科 like to later iterate through it and format the items into a comma separated string with no spaces between the items.


String returnedItems = "a,b,c";
List<String> sellItems = Arrays.asList(returnedItems.split(","));

Now iterate over the list and append each item to a StringBuilder:

StringBuilder sb = new StringBuilder();
for(String item: sellItems){
    if(sb.length() > 0){
        sb.append(',');
    }
    sb.append(item);
}
String result = sb.toString();


One-liners are always popular:

Collections.addAll(arrayList, input.split(","));


split and asList do the trick:

String [] strings = returnedItems.split(",");
List<String> list = Arrays.asList(strings);


Simple one-liner:

selItemArrayList.addAll(Arrays.asList(returnedItems.split("\\s*,\\s*")));

Of course it will be more complex if you have entries with commas in them.


This can help:

for (String s : returnedItems.split(",")) {
    selItemArrayList.add(s.trim());
}

//Shorter and sweeter
String [] strings = returnedItems.split(",");
selItemArrayList = Arrays.asList(strings);

//The reverse....

StringBuilder sb = new StringBuilder();
Iterator<String> iter = selItemArrayList.iterator();
while (iter.hasNext()) {
    if (sb.length() > 0) 
        sb.append(",");
    sb.append(iter.next());
}

returnedItems = sb.toString();


If the strings themselves can have commas in them, things get more complicated. Rather than rolling your own, consider using one of the many open-source CSV parsers. While they are designed to read in files, at least OpenCSV will also parse an individual string you hand it.

  • Commons CSV
  • OpenCSV
  • Super CSV
  • OsterMiller CSV


If the individual items aren't quoted then:

 QString str = "a,,b,c";

 QStringList list1 = str.split(",");
 // list1: [ "a", "", "b", "c" ]

If the items are quoted I'd add "[]" characters and use a JSON parser.


You could use the split() method on String to convert the String to an array that you could loop through.

Although you might be able to skip the looping and parsing with a regular expression to remove the spaces using replaceAll() on a String.


String list = "one, two, three, four";
String[] items = list.split("\\p{Punct}");
List<String> aList = Arrays.asList(items);
System.out.println("aList = " + aList);
StringBuilder formatted = new StringBuilder();

for (int i = 0; i < items.length; i++)
{
    formatted.append(items[i].trim());
    if (i < items.length - 1) formatted.append(',');
}

System.out.println("formatted = " + formatted.toString());


import com.google.common.base.*;

Iterable<String> items = Splitter.on(",").omitEmptyStrings()
                                 .split("Mango,Apple,Guava");

// Join again!
String itemsString = Joiner.join(",").join(items);


String csv = "Apple, Google, Samsung";

step one : converting comma separate String to array of String

String[] elements = csv.split(",");

step two : convert String array to list of String

 List<String> fixedLenghtList = Arrays.asList(elements);

step three : copy fixed list to an ArrayList ArrayList listOfString = new ArrayList(fixedLenghtList);

System.out.println("list from comma separated String : " + listOfString);

System.out.println("size of ArrayList : " + listOfString.size()); Output :

list of comma separated String : [Apple, Google, Samsung]


size of ArrayList : 3
0

精彩评论

暂无评论...
验证码 换一张
取 消