Here you can find the source of split(String str, String separatorChars)
public static String[] split(String str, String separatorChars)
//package com.java2s; import java.util.Vector; public class Main { public static String[] split(String str, String separatorChars) { return splitWorker(str, separatorChars, -1, false); }// www . j a va2 s. c o m private static String[] splitWorker(String str, String separatorChars, int max, boolean preserveAllTokens) { if (str == null) { return null; } int len = str.length(); if (len == 0) { return new String[] { "" }; } Vector<String> vector = new Vector<String>(); int sizePlus1 = 1; int i = 0; int start = 0; boolean match = false; boolean lastMatch = false; if (separatorChars == null) { while (i < len) { if (str.charAt(i) == '\r' || str.charAt(i) == '\n' || str.charAt(i) == '\t') { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } vector.addElement(str.substring(start, i)); match = false; } start = ++i; } else { lastMatch = false; match = true; i++; } } } else if (separatorChars.length() == 1) { char sep = separatorChars.charAt(0); while (i < len) { if (str.charAt(i) == sep) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } vector.addElement(str.substring(start, i)); match = false; } start = ++i; } else { lastMatch = false; match = true; i++; } } } else { while (i < len) { int id = i + separatorChars.length() < len ? i + separatorChars.length() : len; if (separatorChars.indexOf(str.charAt(i)) >= 0 && separatorChars.equals(str.substring(i, id))) { if (match || preserveAllTokens) { lastMatch = true; if (sizePlus1++ == max) { i = len; lastMatch = false; } vector.addElement(str.substring(start, i)); match = false; } i += separatorChars.length(); start = i; } else { lastMatch = false; match = true; i++; } } } if (match || preserveAllTokens && lastMatch) { vector.addElement(str.substring(start, i)); } String[] ret = new String[vector.size()]; vector.copyInto(ret); return ret; } }