您可以执行以下操作:
private enum SpecialChars{ COMMA(","), APOSTROPHE("'"), OPEN_PAREN("("), CLOSE_PAREN(")"); private String value; private SpecialChars(String value) { this.value = value; } public String toString() { return this.value; //will return , or ' instead of COMMA or APOSTROPHE }}使用示例:
public static void main(String[] args){ String line = //..read a line from STDIN //check for special characters if(line.equals(SpecialChars.COMMA) || line.equals(SpecialChars.APOSTROPHE) || line.equals(SpecialChars.OPEN_PAREN) || line.equals(SpecialChars.CLOSE_PAREN) ) { //do something for the special chars }}


