Java - Write code to split a string by a delimiter

Requirements

Write code to split a string by a delimiter

Demo

//package com.book2s;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        String delim = "book2s.com";
        System.out.println(java.util.Arrays.toString(split(str, delim)));
    }/* w  w w. jav  a2 s .c o m*/

    public static final String[] EMPTY_STRINGS = new String[0];

    public static String[] split(final String str, final String delim) {
        if (isEmpty(str)) {
            return EMPTY_STRINGS;
        }
        final List<Object> list = new ArrayList<Object>();
        final StringTokenizer st = new StringTokenizer(str, delim);
        while (st.hasMoreElements()) {
            list.add(st.nextElement());
        }
        return (String[]) list.toArray(new String[list.size()]);
    }

    public static final boolean isEmpty(final String text) {
        return text == null || text.length() == 0;
    }
}

Related Exercise