Java String Split by Separator splitNotRegex(String str, String separatorChars)

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

Description

replace the method : String.split(String regex)

License

Open Source License

Parameter

Parameter Description
str a parameter
separatorChars a parameter

Declaration

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

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.util.ArrayList;

public class Main {
    public static final String[] EMPTY_STRING_ARRAY = new String[0];
    public static final String EMPTY = "";

    /**/*  ww  w  . j a v a2  s. c  o m*/
     * replace the method : String.split(String regex)
      * 
     * @param str
     * @param separatorChars
     * @return
     */
    public static String[] splitNotRegex(String str, String separatorChars) {
        if (str == null) {
            return null;
        }

        int len = str.length();

        if (len == 0) {
            return EMPTY_STRING_ARRAY;
        }

        int separatorLength = separatorChars.length();

        ArrayList<String> substrings = new ArrayList<String>();
        int beg = 0;
        int end = 0;
        while (end < len) {
            end = str.indexOf(separatorChars, beg);

            if (end > -1) {
                if (end > beg) {
                    substrings.add(str.substring(beg, end));
                    beg = end + separatorLength;
                } else {
                    substrings.add(EMPTY);
                    beg = end + separatorLength;
                }
            } else {
                substrings.add(str.substring(beg));
                end = len;
            }
        }

        int resultSize = substrings.size();
        String[] result = substrings.toArray(new String[resultSize]);

        while (resultSize > 0 && substrings.get(resultSize - 1).equals("")) {
            resultSize--;
            // Setting data to null for empty string in last columns to keep original behavior
            result[resultSize] = null;
        }
        return result;
    }
}

Related

  1. splitDoubles(String str, String fieldSeparator)
  2. splitField(String fieldWithSeparator, String separator)
  3. splitInts(String string, String separator)
  4. splitList(String source, char separator)
  5. splitListBySeparator(String text, String separator)
  6. splitSmart(String s, char separator)
  7. splitStaySeparator(String str, char token)
  8. splitString(String sInput, String sSeparator)
  9. splitStringToLong(String strInput, String separator)