Java - Write code to Split the string into parts considering the delimiter given as parameter.

Requirements

Write code to Split the string into parts considering the delimiter given as parameter.

Demo

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

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        String delimiter = ".";
        System.out//ww  w  .j  ava2  s  . com
                .println(java.util.Arrays.toString(split(str, delimiter)));
    }

    /**
     * Splits the string into parts considering the delimiter given as parameter.
     * 
     * @param str String to be split.
     * @param delimiter String used as delimiter.
     * 
     * @return Array of strings.
     */
    public static String[] split(final String str, final String delimiter) {
        StringTokenizer st = new StringTokenizer(str, delimiter);
        String[] splitlist = new String[st.countTokens()];

        for (int i = 0; i < splitlist.length; i++) {
            splitlist[i] = st.nextToken();
        }

        return splitlist;
    }
}

Related Exercise