enum CommentState {NORMAL, COMMMENT_START, MULTI_COMMENT, SIGNLE_COMMENT, COMMENT_ENDING, COMMENT_END, STR, SINGLE_STR};
public static String removeComment(String s) {
int len = s.length();
StringBuilder sb = new StringBuilder(len);
char c;
CommentState state = CommentState.NORMAL;
for(int i = 0; i < len; i++) {
c = s.charAt(i);
switch(c) {
case '/':
if(state == CommentState.NORMAL) {
state = CommentState.COMMMENT_START;
} else if(state == CommentState.COMMMENT_START){
state = CommentState.SIGNLE_COMMENT;
} else if(state == CommentState.COMMENT_ENDING){
state = CommentState.COMMENT_END;
}
break;
case '*':
if(state == CommentState.COMMMENT_START) {
state = CommentState.MULTI_COMMENT;
} else if(state == CommentState.MULTI_COMMENT){
state = CommentState.COMMENT_ENDING;
}
break;
case '\r':
case '\n':
if(state == CommentState.SIGNLE_COMMENT) {
state = CommentState.NORMAL;
} else if(state == CommentState.COMMENT_ENDING){
sb.append('*'); //恢复注释状态
state = CommentState.MULTI_COMMENT;
}
break;
case '"':
if(state == CommentState.NORMAL) {
state = CommentState.STR;
} else if(state == CommentState.STR) {
state = CommentState.NORMAL;
}
break;
case '\'':
if(state == CommentState.NORMAL) {
state = CommentState.SINGLE_STR;
} else if(state == CommentState.SINGLE_STR) {
state = CommentState.NORMAL;
}
break;
default:
if(state == CommentState.COMMENT_ENDING){
sb.append('*');
state = CommentState.MULTI_COMMENT;
}
break;
}
if(state == CommentState.NORMAL
|| state == CommentState.STR
|| state == CommentState.SINGLE_STR) {
sb.append(c);
} else if(state == CommentState.COMMENT_END) {
state = CommentState.NORMAL;
}
}
return sb.toString();
}
public static String removeComment(String s) {
int len = s.length();
StringBuilder sb = new StringBuilder(len);
char c;
CommentState state = CommentState.NORMAL;
for(int i = 0; i < len; i++) {
c = s.charAt(i);
switch(c) {
case '/':
if(state == CommentState.NORMAL) {
state = CommentState.COMMMENT_START;
} else if(state == CommentState.COMMMENT_START){
state = CommentState.SIGNLE_COMMENT;
} else if(state == CommentState.COMMENT_ENDING){
state = CommentState.COMMENT_END;
}
break;
case '*':
if(state == CommentState.COMMMENT_START) {
state = CommentState.MULTI_COMMENT;
} else if(state == CommentState.MULTI_COMMENT){
state = CommentState.COMMENT_ENDING;
}
break;
case '\r':
case '\n':
if(state == CommentState.SIGNLE_COMMENT) {
state = CommentState.NORMAL;
} else if(state == CommentState.COMMENT_ENDING){
sb.append('*'); //恢复注释状态
state = CommentState.MULTI_COMMENT;
}
break;
case '"':
if(state == CommentState.NORMAL) {
state = CommentState.STR;
} else if(state == CommentState.STR) {
state = CommentState.NORMAL;
}
break;
case '\'':
if(state == CommentState.NORMAL) {
state = CommentState.SINGLE_STR;
} else if(state == CommentState.SINGLE_STR) {
state = CommentState.NORMAL;
}
break;
default:
if(state == CommentState.COMMENT_ENDING){
sb.append('*');
state = CommentState.MULTI_COMMENT;
}
break;
}
if(state == CommentState.NORMAL
|| state == CommentState.STR
|| state == CommentState.SINGLE_STR) {
sb.append(c);
} else if(state == CommentState.COMMENT_END) {
state = CommentState.NORMAL;
}
}
return sb.toString();
}