Java String Tokenize getTokens(String string, String tokenSeparator)

Here you can find the source of getTokens(String string, String tokenSeparator)

Description

Tokenizes the given string with the given separator string.

License

Open Source License

Parameter

Parameter Description
string the string to be tokenized
tokenSeparator the separator string

Return

the tokens

Declaration

public static String[] getTokens(String string, String tokenSeparator) 

Method Source Code

//package com.java2s;
/*/*from   www.j  ava2s  .c  o  m*/
 * This file is part of GenoViewer.
 *
 * GenoViewer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GenoViewer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GenoViewer.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Tokenizes the given string with the given separator string.
     * 
     * @param string
     *            the string to be tokenized
     * @param tokenSeparator
     *            the separator string
     * @return the tokens
     */
    public static String[] getTokens(String string, String tokenSeparator) {
        List<Integer> tokenEndIndices = new ArrayList<Integer>();
        int fromIndex = 0;
        while ((fromIndex = string.indexOf(tokenSeparator, fromIndex)) != -1) {
            tokenEndIndices.add(fromIndex++);
        }
        if ((tokenEndIndices.isEmpty())
                || (string.length() > tokenEndIndices.get(tokenEndIndices
                        .size() - 1) + 1))
            tokenEndIndices.add(string.length());

        fromIndex = -tokenSeparator.length();
        String array[] = new String[tokenEndIndices.size()];
        for (int i = 0; i < array.length; ++i) {
            fromIndex += tokenSeparator.length();
            array[i] = string.substring(fromIndex, tokenEndIndices.get(i));
            fromIndex = tokenEndIndices.get(i);
        }

        return array;
    }
}

Related

  1. getTokens(String msg, String delim)
  2. getTokens(String str, String delim)
  3. getTokens(String str, String delimiter)
  4. getTokens(String string)
  5. getTokens(String string)
  6. getTokens(String value)
  7. getTokens(String vbt)
  8. getTokenTypes(CommonTree tree)
  9. getUnitType(String typeToken)