convert String "a,b,c" to List["a","b","c"] - Java java.util

Java examples for java.util:List Convert

Description

convert String "a,b,c" to List["a","b","c"]

Demo Code


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import org.apache.log4j.Logger;

public class Main{
    public static void main(String[] argv){
        String arrString = "book2s.com";
        String delimiter = "book2s.com";
        System.out.println(extractStringToList(arrString,delimiter));
    }/*from  w  w  w  .ja  va 2s  .c o m*/
    /**
     * * convert String "a,b,c" to List["a","b","c"] **
     */
    public static List extractStringToList(String arrString,
            String delimiter) {
        if (arrString == null || "".equals(arrString)) {
            return null;
        }
        StringTokenizer token = new StringTokenizer(arrString, delimiter);
        List list = new ArrayList();
        while (token.hasMoreTokens()) {
            list.add(token.nextToken());
        }
        return list;
    }
}

Related Tutorials