Here you can find the source of split(String what, char delim)
static public String[] split(String what, char delim)
//package com.java2s; /**//w ww .j ava2 s . c o m * Copyright (c) 2010 Ben Fry and Casey Reas * * 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.opensource.org/licenses/eclipse-1.0.php */ import java.util.ArrayList; public class Main { /** * Split a string into pieces along a specific character. * Most commonly used to break up a String along a space or a tab * character. * <P> * This operates differently than the others, where the * single delimeter is the only breaking point, and consecutive * delimeters will produce an empty string (""). This way, * one can split on tab characters, but maintain the column * alignments (of say an excel file) where there are empty columns. */ static public String[] split(String what, char delim) { // do this so that the exception occurs inside the user's // program, rather than appearing to be a bug inside split() if (what == null) return null; //return split(what, String.valueOf(delim)); // huh char chars[] = what.toCharArray(); int splitCount = 0; //1; for (int i = 0; i < chars.length; i++) { if (chars[i] == delim) splitCount++; } // make sure that there is something in the input string //if (chars.length > 0) { // if the last char is a delimeter, get rid of it.. //if (chars[chars.length-1] == delim) splitCount--; // on second thought, i don't agree with this, will disable //} if (splitCount == 0) { String splits[] = new String[1]; splits[0] = new String(what); return splits; } //int pieceCount = splitCount + 1; String splits[] = new String[splitCount + 1]; int splitIndex = 0; int startIndex = 0; for (int i = 0; i < chars.length; i++) { if (chars[i] == delim) { splits[splitIndex++] = new String(chars, startIndex, i - startIndex); startIndex = i + 1; } } //if (startIndex != chars.length) { splits[splitIndex] = new String(chars, startIndex, chars.length - startIndex); //} return splits; } /** * Split a String on a specific delimiter. Unlike Java's String.split() * method, this does not parse the delimiter as a regexp because it's more * confusing than necessary, and String.split() is always available for * those who want regexp. */ static public String[] split(String what, String delim) { ArrayList<String> items = new ArrayList<String>(); int index; int offset = 0; while ((index = what.indexOf(delim, offset)) != -1) { items.add(what.substring(offset, index)); offset = index + delim.length(); } items.add(what.substring(offset)); String[] outgoing = new String[items.size()]; items.toArray(outgoing); return outgoing; } }