Java - Write code to split a string and return a Vector

Requirements

Write code to split a string and return a Vector

Demo

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

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        char delim = '.';
        System.out.println(split(str, delim));
    }/*w  w  w.  j a  v  a2  s  .  c  om*/

    public static Vector split(String str, char delim) {
        Vector tokens = new Vector();
        String token = "";
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == delim) {
                tokens.addElement(token);
                token = "";
            } else {
                token += str.charAt(i);
            }
        }
        if (token.length() > 0) {
            tokens.addElement(token);
        }
        return tokens;
    }
}

Related Exercise