public static String [ ] split ( final String string , final String delim , final int limit ) { // get the count of delim in string, if count is > limit // then use limit for count. The number of delimiters is less by one // than the number of elements, so add one to count. int count = count ( string , delim ) + 1 ; if ( limit > 0 && count > limit ) { count = limit ; } String strings [ ] = new String [ count ] ; int begin = 0 ; for ( int i = 0 ; i < count ; i ++ ) { // get the next index of delim int end = string . indexOf ( delim , begin ) ; // if the end index is -1 or if this is the last element // then use the string's length for the end index if ( end == - 1 || i + 1 == count ) end = string . length ( ) ; // if end is 0, then the first element is empty if ( end == 0 ) strings [ i ] = EMPTY ; else strings [ i ] = string . substring ( begin , end ) ; // update the begining index begin = end + 1 ; } return strings ; }
Split up a string into multiple strings based on a delimiter .