Android String Tokenize tokenize(String input, String token)

Here you can find the source of tokenize(String input, String token)

Description

tokenize

License

Open Source License

Declaration

public static String[] tokenize(String input, String token) 

Method Source Code

//package com.java2s;
/**//from ww  w .j a  v  a 2 s  .co m
 * Copyright (c) {2003,2011} {openmobster@gmail.com} {individual contributors as indicated by the @authors tag}.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */

import java.util.Vector;

public class Main {
    public static String[] tokenize(String input, String token) {
        String[] tokens = null;

        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

  1. tokens(String stringrep, String separatorChars, boolean trim, boolean ignoreBlanks)