Here you can find the source of split(String s, char divider, String[] output)
Parameter | Description |
---|---|
s | the string to split |
divider | the character on which to split. Occurrences of this character are not included in the output |
output | an array to receive the substrings between instances of divider. It must be large enough on entry to accomodate all output. Adjacent instances of the divider character will place empty strings into output. Before returning, output is padded out with empty strings. |
public static void split(String s, char divider, String[] output)
//package com.java2s; // License & terms of use: http://www.unicode.org/copyright.html#License import java.util.ArrayList; public class Main { /**//www. j av a2 s . c om * Split a string into pieces based on the given divider character * @param s the string to split * @param divider the character on which to split. Occurrences of * this character are not included in the output * @param output an array to receive the substrings between * instances of divider. It must be large enough on entry to * accomodate all output. Adjacent instances of the divider * character will place empty strings into output. Before * returning, output is padded out with empty strings. */ public static void split(String s, char divider, String[] output) { int last = 0; int current = 0; int i; for (i = 0; i < s.length(); ++i) { if (s.charAt(i) == divider) { output[current++] = s.substring(last, i); last = i + 1; } } output[current++] = s.substring(last, i); while (current < output.length) { output[current++] = ""; } } /** * Split a string into pieces based on the given divider character * @param s the string to split * @param divider the character on which to split. Occurrences of * this character are not included in the output * @return output an array to receive the substrings between * instances of divider. Adjacent instances of the divider * character will place empty strings into output. */ public static String[] split(String s, char divider) { int last = 0; int i; ArrayList<String> output = new ArrayList<String>(); for (i = 0; i < s.length(); ++i) { if (s.charAt(i) == divider) { output.add(s.substring(last, i)); last = i + 1; } } output.add(s.substring(last, i)); return output.toArray(new String[output.size()]); } }