public static String stripMultiLineComments ( String text ) { if ( text == null ) { return null ; } try { StringBuffer output = new StringBuffer ( ) ; // Comment rules: /*/ This is still a comment /* /* */ // Comments do not nest // /* */ This is in a comment /* // */ // The second // is needed to make this a comment. // First we strip multi line comments. I think this is important: boolean inMultiLine = false ; BufferedReader in = new BufferedReader ( new StringReader ( text ) ) ; while ( true ) { String line = in . readLine ( ) ; if ( line == null ) { break ; } if ( ! inMultiLine ) { // We are not in a multi-line comment, check for a start int cstart = line . indexOf ( COMMENT_MULTILINE_START ) ; if ( cstart >= 0 ) { // This could be a MLC on one line ... int cend = line . indexOf ( COMMENT_MULTILINE_END , cstart + COMMENT_MULTILINE_START . length ( ) ) ; if ( cend >= 0 ) { // A comment that starts and ends on one line // BUG: you can have more than 1 multi-line comment on a line line = line . substring ( 0 , cstart ) + SPACE + line . substring ( cend + COMMENT_MULTILINE_END . length ( ) ) ; } else { // A real multi-line comment inMultiLine = true ; line = line . substring ( 0 , cstart ) + SPACE ; } } else { // We are not in a multi line comment and we havn't // started one so we are going to ignore closing // comments even if they exist. } } else { // We are in a multi-line comment, check for the end int cend = line . indexOf ( COMMENT_MULTILINE_END ) ; if ( cend >= 0 ) { // End of comment line = line . substring ( cend + COMMENT_MULTILINE_END . length ( ) ) ; inMultiLine = false ; } else { // The comment continues line = SPACE ; } } output . append ( line ) ; output . append ( ' ' ) ; } return output . toString ( ) ; } catch ( IOException e ) { throw new IllegalArgumentException ( e ) ; } }
Remove all the multi - line comments from a block of text