Here you can find the source of split(String source, String separator)
public static String[] split(String source, String separator) throws NullPointerException
//package com.java2s; public class Main { public static String[] split(String source, String separator) throws NullPointerException { String[] returnVal = null; int cnt = 1; int index = source.indexOf(separator); int index0 = 0; while (index >= 0) { cnt++;//from w ww .j a va2s . c o m index = source.indexOf(separator, index + 1); } returnVal = new String[cnt]; cnt = 0; index = source.indexOf(separator); while (index >= 0) { returnVal[cnt] = source.substring(index0, index); index0 = index + 1; index = source.indexOf(separator, index + 1); cnt++; } returnVal[cnt] = source.substring(index0); return returnVal; } public static String[] split(String source, String separator, int arraylength) throws NullPointerException { String[] returnVal = new String[arraylength]; int cnt = 0; int index0 = 0; int index = source.indexOf(separator); while (index >= 0 && cnt < (arraylength - 1)) { returnVal[cnt] = source.substring(index0, index); index0 = index + 1; index = source.indexOf(separator, index + 1); cnt++; } returnVal[cnt] = source.substring(index0); if (cnt < (arraylength - 1)) { for (int i = cnt + 1; i < arraylength; i++) { returnVal[i] = ""; } } return returnVal; } public static int indexOf(String str, String searchStr) { if (str == null || searchStr == null) { return -1; } return str.indexOf(searchStr); } }