Java String Split splitEx(String str, String spilter)

Here you can find the source of splitEx(String str, String spilter)

Description

split Ex

License

Apache License

Declaration

public static String[] splitEx(String str, String spilter) 

Method Source Code

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

import java.util.ArrayList;

public class Main {
    public static String[] splitEx(String str, String spilter) {
        if (str == null) {
            return null;
        }/*from  w w  w .jav  a2 s .  co  m*/
        if ((spilter == null) || (spilter.equals("")) || (str.length() < spilter.length())) {
            String[] t = { str };
            return t;
        }
        ArrayList al = new ArrayList();
        char[] cs = str.toCharArray();
        char[] ss = spilter.toCharArray();
        int length = spilter.length();
        int lastIndex = 0;
        for (int i = 0; i <= str.length() - length;) {
            boolean notSuit = false;
            for (int j = 0; j < length; ++j) {
                if (cs[(i + j)] != ss[j]) {
                    notSuit = true;
                    break;
                }
            }
            if (!(notSuit)) {
                al.add(str.substring(lastIndex, i));
                i += length;
                lastIndex = i;
            } else {
                ++i;
            }
        }
        if (lastIndex <= str.length()) {
            al.add(str.substring(lastIndex, str.length()));
        }
        String[] t = new String[al.size()];
        for (int i = 0; i < al.size(); ++i) {
            t[i] = ((String) al.get(i));
        }
        return t;
    }

    public static String subString(String src, int length) {
        if (src == null) {
            return null;
        }
        int i = src.length();
        if (i > length) {
            return src.substring(0, length);
        }
        return src;
    }
}

Related

  1. splitCQLStatements(String source)
  2. splitDateText(String text)
  3. splitDigits(long input)
  4. splitDomains(String domains)
  5. splitElements(String values)
  6. splitFields(String fieldsString, int minNum)
  7. splitForIndexMatching(String string)
  8. splitGenericIfNeeded(String name)
  9. splitHelperName(String name)