Java String Split split(final String string, final String toSplit)

Here you can find the source of split(final String string, final String toSplit)

Description

Splits the passed string based on the toSplit string.

License

Common Public License

Declaration

public static List<String> split(final String string, final String toSplit) 

Method Source Code

//package com.java2s;
/*/*from ww w.j  av  a 2  s .c  o m*/
 * @author Fabio Zadrozny
 * Created: June 2005
 * License: Common Public License v1.0
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Splits the passed string based on the toSplit string.
     */
    public static List<String> split(final String string, final String toSplit) {
        if (toSplit.length() == 1) {
            return split(string, toSplit.charAt(0));
        }
        ArrayList<String> ret = new ArrayList<String>();
        if (toSplit.length() == 0) {
            ret.add(string);
            return ret;
        }

        int len = string.length();

        int last = 0;

        char c = 0;

        for (int i = 0; i < len; i++) {
            c = string.charAt(i);
            if (c == toSplit.charAt(0) && matches(string, toSplit, i)) {
                if (last != i) {
                    ret.add(string.substring(last, i));
                }
                last = i + toSplit.length();
                i += toSplit.length() - 1;
            }
        }

        if (last < len) {
            ret.add(string.substring(last, len));
        }

        return ret;
    }

    /**
     * Splits some string given some char
     */
    public static List<String> split(String string, char toSplit) {
        ArrayList<String> ret = new ArrayList<String>();
        int len = string.length();

        int last = 0;

        char c = 0;

        for (int i = 0; i < len; i++) {
            c = string.charAt(i);
            if (c == toSplit) {
                if (last != i) {
                    ret.add(string.substring(last, i));
                }
                while (c == toSplit && i < len - 1) {
                    i++;
                    c = string.charAt(i);
                }
                last = i;
            }
        }
        if (c != toSplit) {
            if (last == 0 && len > 0) {
                ret.add(string); // it is equal to the original (no dots)

            } else if (last < len) {
                ret.add(string.substring(last, len));

            }
        }
        return ret;
    }

    /**
     * Splits some string given many chars
     */
    public static List<String> split(String string, char... toSplit) {
        ArrayList<String> ret = new ArrayList<String>();
        int len = string.length();

        int last = 0;

        char c = 0;

        for (int i = 0; i < len; i++) {
            c = string.charAt(i);

            if (contains(c, toSplit)) {
                if (last != i) {
                    ret.add(string.substring(last, i));
                }
                while (contains(c, toSplit) && i < len - 1) {
                    i++;
                    c = string.charAt(i);
                }
                last = i;
            }
        }
        if (!contains(c, toSplit)) {
            if (last == 0 && len > 0) {
                ret.add(string); // it is equal to the original (no dots)

            } else if (last < len) {
                ret.add(string.substring(last, len));

            }
        }
        return ret;
    }

    private static boolean matches(String string, String toSplit, int i) {
        if (string.length() - i >= toSplit.length()) {
            for (int j = 0; j < toSplit.length(); j++) {
                if (string.charAt(i + j) != toSplit.charAt(j)) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    private static boolean contains(char c, char[] toSplit) {
        for (char ch : toSplit) {
            if (c == ch) {
                return true;
            }
        }
        return false;
    }
}

Related

  1. split(final String s)
  2. split(final String str)
  3. split(final String str)
  4. split(final String string)
  5. split(final String string)
  6. split(final String text, final String sp)
  7. split(final String toSplit)
  8. split(Iterable objects, boolean even)
  9. split(String arg)