split string to String array - Java java.lang

Java examples for java.lang:String Split

Description

split string to String array

Demo Code


//package com.java2s;

import java.util.Vector;

public class Main {
    public static void main(String[] argv) {
        String input = "java2s.com";
        char delimiter = '.';
        System.out.println(java.util.Arrays
                .toString(split(input, delimiter)));
    }/*from   w  w  w. ja  v a 2 s . com*/

    public static String[] split(String input, char delimiter) {
        Vector v = new Vector();
        boolean moreTokens = true;
        String subString;

        while (moreTokens) {
            int tokenLocation = input.indexOf(delimiter);
            if (tokenLocation > 0) {
                subString = input.substring(0, tokenLocation);
                v.addElement(subString);
                input = input.substring(tokenLocation + 1);
            } else {
                moreTokens = false;
                v.addElement(input);
            }
        }

        String[] res = new String[v.size()];

        for (int i = 0; i != res.length; i++) {
            res[i] = (String) v.elementAt(i);
        }
        return res;
    }
}

Related Tutorials