Java String Split by Char splitWithEscape(String s, String splitChars)

Here you can find the source of splitWithEscape(String s, String splitChars)

Description

split With Escape

License

Apache License

Declaration

public static List<String> splitWithEscape(String s, String splitChars) 

Method Source Code

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

import java.util.*;

public class Main {
    public static final char escapeChar = '\\';

    public static List<String> splitWithEscape(String s, String splitChars) {
        if (splitChars == null) {
            splitChars = "";
        }/*from w w  w  .  j  av  a2 s  .co  m*/
        List<String> result = new ArrayList<String>();
        if (s == null) {
            return result;
        }
        boolean escaped = false;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            char ch = (char) s.codePointAt(i);
            if (escaped) {
                sb.append(ch);
                escaped = false;
            } else {
                if (ch == escapeChar) {
                    escaped = true;
                    continue;
                } else {
                    if (splitChars.indexOf(ch) > -1) {
                        result.add(sb.toString());
                        sb = new StringBuilder();
                    } else {
                        sb.append(ch);
                    }
                }
            }
        }
        result.add(sb.toString());
        return result;
    }
}

Related

  1. splitString(final String s, final char c, final boolean trimBlanks)
  2. splitString(String str, char sep)
  3. splitString(String string, char ch)
  4. splitStringToChars(String str)
  5. splitTrim(String str, String limitChar)