Searching...
Friday, 25 October 2013

How to convert comma separated String to ArrayList ?

10:04

Example

package com.pretech;
import java.util.*;
public class StringtoListExample {
	public static void main(String[] args) {
		System.out.println(getCommaSeparatedStringAsList("Welcome,to,pretech,blog"));
	}
	public static List getCommaSeparatedStringAsList(String commaSeparatedString) {
		List result = new ArrayList();
		if ((commaSeparatedString != null)
				&& (commaSeparatedString.length() > 0)) {
			StringTokenizer tokenizer = new StringTokenizer(
					commaSeparatedString, ",");
			while (tokenizer.hasMoreTokens()) {
				result.add(tokenizer.nextToken().trim());
			}
		}
		return result;
	}
}

Output



[Welcome, to, pretech, blog]


0 comments: