Android String Split split(String str, String separatorChars)

Here you can find the source of split(String str, String separatorChars)

Description

split

Declaration

public static String[] split(String str, String separatorChars) 

Method Source Code

//package com.java2s;
import java.util.Vector;

public class Main {

    public static String[] split(String str, String separatorChars) {
        return splitWorker(str, separatorChars, -1, false);
    }// www  .  j a va2 s.  c  o m

    private static String[] splitWorker(String str, String separatorChars,
            int max, boolean preserveAllTokens) {
        if (str == null) {
            return null;
        }
        int len = str.length();
        if (len == 0) {
            return new String[] { "" };
        }
        Vector<String> vector = new Vector<String>();
        int sizePlus1 = 1;
        int i = 0;
        int start = 0;
        boolean match = false;
        boolean lastMatch = false;
        if (separatorChars == null) {
            while (i < len) {
                if (str.charAt(i) == '\r' || str.charAt(i) == '\n'
                        || str.charAt(i) == '\t') {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        vector.addElement(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                } else {
                    lastMatch = false;
                    match = true;
                    i++;
                }
            }
        } else if (separatorChars.length() == 1) {
            char sep = separatorChars.charAt(0);
            while (i < len) {
                if (str.charAt(i) == sep) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        vector.addElement(str.substring(start, i));
                        match = false;
                    }
                    start = ++i;
                } else {
                    lastMatch = false;
                    match = true;
                    i++;
                }
            }
        } else {
            while (i < len) {
                int id = i + separatorChars.length() < len ? i
                        + separatorChars.length() : len;
                if (separatorChars.indexOf(str.charAt(i)) >= 0
                        && separatorChars.equals(str.substring(i, id))) {
                    if (match || preserveAllTokens) {
                        lastMatch = true;
                        if (sizePlus1++ == max) {
                            i = len;
                            lastMatch = false;
                        }
                        vector.addElement(str.substring(start, i));
                        match = false;
                    }
                    i += separatorChars.length();
                    start = i;
                } else {
                    lastMatch = false;
                    match = true;
                    i++;
                }
            }
        }

        if (match || preserveAllTokens && lastMatch) {
            vector.addElement(str.substring(start, i));
        }
        String[] ret = new String[vector.size()];
        vector.copyInto(ret);
        return ret;
    }
}

Related

  1. split(String source, String separator, int arraylength)
  2. split(String str, String delims, boolean trimTokens)
  3. stringToListStr(String input)
  4. splitStringOnWhitespace(String stringToSplit)
  5. stringToListByLine(final String fileContent)
  6. split(String str, char delimiter)
  7. split(String str, char separator)
  8. parseDelimitedList(String list, char delimiter)
  9. splitIt(String s)