Java String Split by Separator splitWithoutEscaped(String str, char separatorChar)

Here you can find the source of splitWithoutEscaped(String str, char separatorChar)

Description

split Without Escaped

License

Apache License

Declaration

public static String[] splitWithoutEscaped(String str,
            char separatorChar) 

Method Source Code

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

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static String[] splitWithoutEscaped(String str,
            char separatorChar) {
        return splitWithoutEscaped(str, separatorChar, false);
    }//from w ww. j  a  va 2 s. c om

    /**
     * does not take into account escaped separators
     */
    public static String[] splitWithoutEscaped(String str,
            char separatorChar, boolean retainEmpty) {
        int len = str.length();
        if (len == 0) {
            return new String[0];
        }
        List<String> list = new ArrayList<String>();
        int i = 0;
        int start = 0;
        boolean match = false;
        while (i < len) {
            if (str.charAt(i) == '\\') {
                match = true;
                i += 2;
            } else if (str.charAt(i) == separatorChar) {
                if (retainEmpty || match) {
                    list.add(str.substring(start, i));
                    match = false;
                }
                start = ++i;
            } else {
                match = true;
                i++;
            }
        }
        if (retainEmpty || match) {
            list.add(str.substring(start, i));
        }
        return list.toArray(new String[list.size()]);
    }
}

Related

  1. splitStructs(String str, char separator)
  2. splitToList(String s, String separator)
  3. splitValues(String concatedValues, String separator)
  4. splitValues(String values, String separator)
  5. splitWithBreakingBrace(String str, char separator, char braceStart, char braceEnd)
  6. splitWorker(final String str, final char separatorChar, final boolean preserveAllTokens)
  7. stringSplit(String string, int separator, int escape)