public static String stripComments(String src,String stringOpens,String stringCloses,boolean slashStarComments,boolean slashSlashComments,boolean hashComments,boolean dashDashComments){
  if (src == null) {
    return null;
  }
  StringBuilder strBuilder=new StringBuilder(src.length());
  StringReader sourceReader=new StringReader(src);
  int contextMarker=Character.MIN_VALUE;
  boolean escaped=false;
  int markerTypeFound=-1;
  int ind=0;
  int currentChar=0;
  try {
    while ((currentChar=sourceReader.read()) != -1) {
      if (markerTypeFound != -1 && currentChar == stringCloses.charAt(markerTypeFound) && !escaped) {
        contextMarker=Character.MIN_VALUE;
        markerTypeFound=-1;
      }
 else       if ((ind=stringOpens.indexOf(currentChar)) != -1 && !escaped && contextMarker == Character.MIN_VALUE) {
        markerTypeFound=ind;
        contextMarker=currentChar;
      }
      if (contextMarker == Character.MIN_VALUE && currentChar == '/' && (slashSlashComments || slashStarComments)) {
        currentChar=sourceReader.read();
        if (currentChar == '*' && slashStarComments) {
          int prevChar=0;
          while ((currentChar=sourceReader.read()) != '/' || prevChar != '*') {
            if (currentChar == '\r') {
              currentChar=sourceReader.read();
              if (currentChar == '\n') {
                currentChar=sourceReader.read();
              }
            }
 else {
              if (currentChar == '\n') {
                currentChar=sourceReader.read();
              }
            }
            if (currentChar < 0) {
              break;
            }
            prevChar=currentChar;
          }
          continue;
        }
 else         if (currentChar == '/' && slashSlashComments) {
          while ((currentChar=sourceReader.read()) != '\n' && currentChar != '\r' && currentChar >= 0) {
          }
        }
      }
 else       if (contextMarker == Character.MIN_VALUE && currentChar == '#' && hashComments) {
        while ((currentChar=sourceReader.read()) != '\n' && currentChar != '\r' && currentChar >= 0) {
        }
      }
 else       if (contextMarker == Character.MIN_VALUE && currentChar == '-' && dashDashComments) {
        currentChar=sourceReader.read();
        if (currentChar == -1 || currentChar != '-') {
          strBuilder.append('-');
          if (currentChar != -1) {
            strBuilder.append(currentChar);
          }
          continue;
        }
        while ((currentChar=sourceReader.read()) != '\n' && currentChar != '\r' && currentChar >= 0) {
        }
      }
      if (currentChar != -1) {
        strBuilder.append((char)currentChar);
      }
    }
  }
 catch (  IOException ioEx) {
  }
  return strBuilder.toString();
}

public static String stripComments ( String src , String stringOpens , String stringCloses , boolean slashStarComments , boolean slashSlashComments , boolean hashComments , boolean dashDashComments ) { if ( src == null ) { return null ; } StringBuilder strBuilder = new StringBuilder ( src . length ( ) ) ; StringReader sourceReader = new StringReader ( src ) ; int contextMarker = Character . MIN_VALUE ; boolean escaped = false ; int markerTypeFound = - 1 ; int ind = 0 ; int currentChar = 0 ; try { while ( ( currentChar = sourceReader . read ( ) ) != - 1 ) { if ( markerTypeFound != - 1 && currentChar == stringCloses . charAt ( markerTypeFound ) && ! escaped ) { contextMarker = Character . MIN_VALUE ; markerTypeFound = - 1 ; } else if ( ( ind = stringOpens . indexOf ( currentChar ) ) != - 1 && ! escaped && contextMarker == Character . MIN_VALUE ) { markerTypeFound = ind ; contextMarker = currentChar ; } if ( contextMarker == Character . MIN_VALUE && currentChar == '/' && ( slashSlashComments || slashStarComments ) ) { currentChar = sourceReader . read ( ) ; if ( currentChar == '*' && slashStarComments ) { int prevChar = 0 ; while ( ( currentChar = sourceReader . read ( ) ) != '/' || prevChar != '*' ) { if ( currentChar == '\r' ) { currentChar = sourceReader . read ( ) ; if ( currentChar == '\n' ) { currentChar = sourceReader . read ( ) ; } } else { if ( currentChar == '\n' ) { currentChar = sourceReader . read ( ) ; } } if ( currentChar < 0 ) { break ; } prevChar = currentChar ; } continue ; } else if ( currentChar == '/' && slashSlashComments ) { while ( ( currentChar = sourceReader . read ( ) ) != '\n' && currentChar != '\r' && currentChar >= 0 ) { } } } else if ( contextMarker == Character . MIN_VALUE && currentChar == '#' && hashComments ) { while ( ( currentChar = sourceReader . read ( ) ) != '\n' && currentChar != '\r' && currentChar >= 0 ) { } } else if ( contextMarker == Character . MIN_VALUE && currentChar == '-' && dashDashComments ) { currentChar = sourceReader . read ( ) ; if ( currentChar == - 1 || currentChar != '-' ) { strBuilder . append ( '-' ) ; if ( currentChar != - 1 ) { strBuilder . append ( currentChar ) ; } continue ; } while ( ( currentChar = sourceReader . read ( ) ) != '\n' && currentChar != '\r' && currentChar >= 0 ) { } } if ( currentChar != - 1 ) { strBuilder . append ( ( char ) currentChar ) ; } } } catch ( IOException ioEx ) { } return strBuilder . toString ( ) ; }
returns the given string , with comments removed
