Here you can find the source of getTokens(String string, String tokenSeparator)
Parameter | Description |
---|---|
string | the string to be tokenized |
tokenSeparator | the separator string |
public static String[] getTokens(String string, String tokenSeparator)
//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; } }