Busted Mug

A blog that documents solutions to the most frustrating problems that occur during development in technologies such as Java, XML, AJAX, SQL, CSS and others that make me want to throw my coffee mug against the cube wall.

Tuesday, November 28, 2006

Comma separated lists in java

I find myself doing this for a user a million times and have taken until now to take care of the formatting somewhere else. If you need a list that has commas in between items, but of course doesn't have a trailing comma, something like this is handy:
  * bch: formats a comma-separated list
  * @param items string separated by spaces
  * @return list in format a, b, c
 public static String commalist(String items) {
  StringTokenizer st = new StringTokenizer(items);
  StringBuffer sb = new StringBuffer();
  while (st.hasMoreElements()) {
     if (st.hasMoreElements()) {
    sb.append(", ");
  return sb.toString();


At 3:38 AM, Blogger Sid L said...


Useful article. Very well explained. Thanks for sharing.



Post a Comment

<< Home