static public List < String > backslashsplit ( String s , char sep ) { List < String > path = new ArrayList < String > ( ) ; int len = s . length ( ) ; StringBuilder piece = new StringBuilder ( ) ; int i = 0 ; for ( ; i <= len - 1 ; i ++ ) { char c = s . charAt ( i ) ; if ( c == ' ' && i < ( len - 1 ) ) { piece . append ( c ) ; // keep escapes in place piece . append ( s . charAt ( ++ i ) ) ; } else if ( c == sep ) { path . add ( piece . toString ( ) ) ; piece . setLength ( 0 ) ; } else piece . append ( c ) ; } path . add ( piece . toString ( ) ) ; return path ; }
Split a string with respect to a separator character and taking backslashes into consideration .