0

I have some values in List<SelectItem> selectedDividendYears
Now I want to make a string concatenating all the values of this list as csv.

For Example:

selectedDividendYears = {'123', 'sdf234', '12 1234c'}

now I want only one string, which will look like -

"123, sdf, 12 1234c"
Addicted
  • 1,694
  • 1
  • 16
  • 24

2 Answers2

3

Apache commons-lang, StringUtils.join(selectedDividendYears, ',');.

xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
  • does the data type of selectedDividendYear matter ???means can i use this for any data type. – Addicted May 26 '12 at 08:52
  • It would appear so. join has many different variations: http://commons.apache.org/lang/api-2.5/org/apache/commons/lang/StringUtils.html#join%28java.util.Collection,%20char%29 – Jeremy Goodell May 26 '12 at 08:55
  • please refer to [this question](http://stackoverflow.com/questions/10811359/sort-items-of-richpicklist-target-table-automatically) and see if u can help me out. – Addicted May 30 '12 at 07:14
1
StringBuilder str = new StringBuilder();
boolean first = true;
for (SelectItem item : selectedDividendYears) {
   if (first) first = false;
   else str.append(",");
   str.append(item.toString());
}

String outputString = str.toString();
Jeremy Goodell
  • 18,225
  • 5
  • 35
  • 52
  • please refer to [this question](http://stackoverflow.com/questions/10811359/sort-items-of-richpicklist-target-table-automatically) and see if u can help me out. – Addicted May 30 '12 at 07:15