Java String Split by Char split(String s, char divider, String[] output)

Here you can find the source of split(String s, char divider, String[] output)

Description

Split a string into pieces based on the given divider character

License

Open Source License

Parameter

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.

Declaration

public static void split(String s, char divider, String[] output) 

Method Source Code

//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()]);
    }
}

Related

  1. split(String s, char c)
  2. split(String s, char c, int limit)
  3. split(String s, char c, int limit)
  4. split(String s, char ch)
  5. split(String s, char divider)
  6. split(String s, char sep)
  7. split(String s, char sep)
  8. split(String splittee, String splitChar)
  9. split(String str, char c)