private String escapeAndQuote ( String value ) { // the initial count is for the preceding and trailing quotes int count = 2 ; for ( int i = 0 ; i < value . length ( ) ; i ++ ) { switch ( value . charAt ( i ) ) { case ' ' : case ' ' : case ' ' : case ' ' : count ++ ; break ; default : break ; } } StringBuffer sb = new StringBuffer ( value . length ( ) + count ) ; sb . append ( strategy . getEncapsulator ( ) ) ; for ( int i = 0 ; i < value . length ( ) ; i ++ ) { char c = value . charAt ( i ) ; if ( c == strategy . getEncapsulator ( ) ) { sb . append ( ' ' ) . append ( c ) ; continue ; } switch ( c ) { case ' ' : sb . append ( "\\n" ) ; break ; case ' ' : sb . append ( "\\r" ) ; break ; case ' ' : sb . append ( "\\\\" ) ; break ; default : sb . append ( c ) ; } } sb . append ( strategy . getEncapsulator ( ) ) ; return sb . toString ( ) ; }
Enclose the value in quotes and escape the quote and comma characters that are inside .