Java String Split split(String str, String id)

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

Description

split

License

Apache License

Declaration

public static ArrayList split(String str, String id) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    public static ArrayList split(String str, String id) {
        ArrayList vt = new ArrayList();
        if ((str != null) && (id != null) && (!str.equals("")) && (!id.equals(""))) {
            int beginindex = 0 - id.length();
            int endindex = 0;
            int end = str.lastIndexOf(id);
            if (end == -1) {
                vt.add(str);/*from   w  w w  .j  ava2s.co  m*/
            } else {
                while (endindex < end) {
                    endindex = str.indexOf(id, beginindex + id.length());
                    vt.add(str.substring(beginindex + id.length(), endindex));
                    beginindex = endindex;
                }
                vt.add(str.substring(endindex + id.length(), str.length()));
            }
        }
        return vt;
        /*
         * ArrayList tmp = new ArrayList(); if ((str == null) || (id == null) ||
         * (str.equals("")) || (id.equals(""))){ return tmp; } String[] t =
         * str.split(id); for (int i = 0; i < t.length; i++){ tmp.add(tmp); }
         * return tmp;
         */
    }

    /**
     * Find the index of the searchStr in the string array strs.
     * Return -1, if not found.
     *
     * <pre>
     * StringUtil.indexOf(["s1", "s2"], "s1", true) = 0
     * StringUtil.indexOf(["s1", "s2"], "S1", true) = -1
     * </pre>
     *
     * @param strs
     *            the string array to check, maybe null.
     * @param searchStr
     *            the string to search for, maybe null.
     * @param caseSensive
     *            is it case sensive while finding the searchStr
     *            in the searchStr.
     * @return index of the searchStr found in the strs, -1 if not found.
     *
     * @author chenke
     */
    public static int indexOf(String[] strs, String searchStr, boolean caseSensive) {

        if (strs == null || strs.length == 0) {
            return -1;
        }
        for (int i = 0; i < strs.length; i++) {
            if (isEqual(strs[i], searchStr, caseSensive)) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOf(String[] strs, String searchStr) {
        return indexOf(strs, searchStr, true);
    }

    private static boolean isEqual(String s1, String s2, boolean caseSensive) {
        if (caseSensive) {
            return s1 == null ? s2 == null : s1.equals(s2);
        } else {
            return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
        }
    }
}

Related

  1. split(String str)
  2. split(String str)
  3. split(String str)
  4. split(String str)
  5. split(String str, int ch)
  6. split(String str, String splitStr)
  7. split(String str, String splitter)
  8. split(String str, String token)
  9. split(String string)