public static List < String > parseDelimitedString ( String value , char delimiter , boolean trim ) { if ( value == null ) { value = "" ; } List < String > list = new ArrayList < String > ( ) ; StringBuilder sb = new StringBuilder ( ) ; int expecting = ( CHAR | DELIMITER | START_QUOTE ) ; for ( int i = 0 ; i < value . length ( ) ; i ++ ) { char character = value . charAt ( i ) ; boolean isEscaped = isEscaped ( value , i ) ; boolean isDelimiter = isDelimiter ( delimiter , character , isEscaped ) ; boolean isQuote = isQuote ( character , isEscaped ) ; if ( isDelimiter && ( ( expecting & DELIMITER ) > 0 ) ) { addPart ( list , sb , trim ) ; sb . delete ( 0 , sb . length ( ) ) ; expecting = ( CHAR | DELIMITER | START_QUOTE ) ; } else if ( isQuote && ( ( expecting & START_QUOTE ) > 0 ) ) { sb . append ( character ) ; expecting = CHAR | END_QUOTE ; } else if ( isQuote && ( ( expecting & END_QUOTE ) > 0 ) ) { sb . append ( character ) ; expecting = ( CHAR | START_QUOTE | DELIMITER ) ; } else if ( ( expecting & CHAR ) > 0 ) { sb . append ( character ) ; } else { String message = String . format ( "Invalid delimited string [%s] for delimiter: %s" , value , delimiter ) ; throw new IllegalArgumentException ( message ) ; } } if ( sb . length ( ) > 0 ) { addPart ( list , sb , trim ) ; } return list ; }
Parses delimited string and returns an array containing the tokens . This parser obeys quotes so the delimiter character will be ignored if it is inside of a quote . This method assumes that the quote character is not included in the set of delimiter characters .