tokenize a string by string token - Android java.lang

Android examples for java.lang:String Split

Description

tokenize a string by string token

Demo Code

import java.util.Vector;

public class Main{

    public static String[] tokenize(String input, String token) {
        String[] tokens = null;/* w ww .j a  v  a  2 s. co m*/

        Vector cour = new Vector();
        if (input.indexOf(token) != -1) {
            int startIndex = 0;
            int endIndex = input.indexOf(token, startIndex);
            do {
                String courToken = input.substring(startIndex, endIndex);
                if (courToken.trim().length() > 0) {
                    cour.addElement(courToken);
                }

                //Recalculate the indices
                startIndex = endIndex + token.length();
                if (startIndex >= input.length() - 1) {
                    break;
                }

                //Calculate the endIndex to get the next token
                endIndex = input.indexOf(token, startIndex);
                if (endIndex == -1) {
                    endIndex = input.length();
                }

            } while (true);
        } else {
            cour.addElement(input);
        }

        tokens = new String[cour.size()];
        for (int i = 0, size = cour.size(); i < size; i++) {
            tokens[i] = (String) cour.elementAt(i);
        }

        return tokens;
    }

}

Related Tutorials