Android String Split split(String source, String separator)

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

Description

split

Declaration

public static String[] split(String source, String separator)
        throws NullPointerException 

Method Source Code

//package com.java2s;

public class Main {

    public static String[] split(String source, String separator)
            throws NullPointerException {
        String[] returnVal = null;
        int cnt = 1;

        int index = source.indexOf(separator);
        int index0 = 0;
        while (index >= 0) {
            cnt++;//from  w ww .j a va2s  .  c o  m
            index = source.indexOf(separator, index + 1);
        }
        returnVal = new String[cnt];
        cnt = 0;
        index = source.indexOf(separator);
        while (index >= 0) {
            returnVal[cnt] = source.substring(index0, index);
            index0 = index + 1;
            index = source.indexOf(separator, index + 1);
            cnt++;
        }
        returnVal[cnt] = source.substring(index0);

        return returnVal;
    }

    public static String[] split(String source, String separator,
            int arraylength) throws NullPointerException {
        String[] returnVal = new String[arraylength];
        int cnt = 0;
        int index0 = 0;
        int index = source.indexOf(separator);
        while (index >= 0 && cnt < (arraylength - 1)) {
            returnVal[cnt] = source.substring(index0, index);
            index0 = index + 1;
            index = source.indexOf(separator, index + 1);
            cnt++;
        }
        returnVal[cnt] = source.substring(index0);
        if (cnt < (arraylength - 1)) {
            for (int i = cnt + 1; i < arraylength; i++) {
                returnVal[i] = "";
            }
        }

        return returnVal;
    }

    public static int indexOf(String str, String searchStr) {
        if (str == null || searchStr == null) {
            return -1;
        }
        return str.indexOf(searchStr);
    }
}

Related

  1. split(String original, String separator)
  2. split(String original, String separator)
  3. split(String s, String sep)
  4. split(String str, String delim)
  5. split(String str, String delimiter)
  6. split(String str, String delims)
  7. split(String str, String separator)