Here you can find the source of split(String string, String token)
public static String[] split(String string, String token)
//package com.java2s; /**************************************************************************** **//from ww w. j a v a2 s . c om ** Copyright (C) 1992-2009 Nokia. All rights reserved. ** ** This file is part of Qt Jambi. ** ** $BEGIN_LICENSE$ ** GNU Lesser General Public License Usage ** This file may be used under the terms of the GNU Lesser ** General Public License version 2.1 as published by the Free Software ** Foundation and appearing in the file LICENSE.LGPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU Lesser General Public License version 2.1 requirements ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. ** ** In addition, as a special exception, Nokia gives you certain ** additional rights. These rights are described in the Nokia Qt LGPL ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this ** package. ** ** GNU General Public License Usage ** Alternatively, this file may be used under the terms of the GNU ** General Public License version 3.0 as published by the Free Software ** Foundation and appearing in the file LICENSE.GPL included in the ** packaging of this file. Please review the following information to ** ensure the GNU General Public License version 3.0 requirements will be ** met: http://www.gnu.org/copyleft/gpl.html. ** $END_LICENSE$ ** ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ** ****************************************************************************/ import java.util.Stack; public class Main { /** * Same as calling split(string, token, 0) */ public static String[] split(String string, String token) { return split(string, token, 0); } /** * Splits a string on a specified token. The token is not * a regular expression. Works around missing String.split() in some * runtime environments and is also a lot faster than using * a regular expression for splitting on regular string tokens. */ public static String[] split(String string, String token, int limit) { Stack<String> parts = new Stack<String>(); int pos = 0, nextPos = 0; do { // Break if limit is reached if (limit > 0 && parts.size() == limit - 1) { parts.push(string.substring(pos)); break; } else { nextPos = string.indexOf(token, pos); String part = nextPos >= 0 ? string.substring(pos, nextPos) : string.substring(pos); // System.err.println("nextPos: " + nextPos + " part: " + part); if (part.length() != 0 || parts.isEmpty() || parts.peek().length() != 0) { parts.push(part); } pos = nextPos + token.length(); // Always progress at least by one to avoid infinite loops if (pos == nextPos) ++pos; } } while (nextPos >= 0 && pos <= string.length()); // No trailing empty elements if limit==0 if (limit == 0 && parts.peek().length() == 0) parts.pop(); return parts.toArray(new String[] {}); } }