public static String [ ] splitPreserveAllTokens ( String value , char separatorChar ) { if ( value == null ) { return ArrayUtils . EMPTY_STRING_ARRAY ; } int len = value . length ( ) ; if ( len == 0 ) { return ArrayUtils . EMPTY_STRING_ARRAY ; } List < String > list = new ArrayList <> ( ) ; int i = 0 ; int start = 0 ; boolean match = false ; boolean lastMatch = false ; int escapeStart = - 2 ; while ( i < len ) { char c = value . charAt ( i ) ; if ( c == ESCAPE_CHAR ) { escapeStart = i ; } if ( c == separatorChar && escapeStart != i - 1 ) { lastMatch = true ; list . add ( value . substring ( start , i ) ) ; match = false ; start = ++ i ; continue ; } match = true ; i ++ ; } if ( match || lastMatch ) { list . add ( value . substring ( start , i ) ) ; } return list . toArray ( new String [ list . size ( ) ] ) ; }
String tokenizer that preservers all tokens and ignores escaped separator chars .