How to check if a character is correct

前端 未结 4 1877
情话喂你
情话喂你 2021-01-23 05:04

I have a bunch of characters and want to remove everything that isn\'t a \'#\' \'.\' \'E\' and \'G\'.

I tried to use this:

if (buffer.get(buffertest) ==          


        
4条回答
  •  故里飘歌
    2021-01-23 05:39

    You need to compare for each character individually. Assuming that buffer.get(buffertest) returns a char, here's how to do it:

    char c = buffer.get(buffertest);
    if (c == 'G' || c == 'E' || c == '#' || c == '.') {
        // do something
    }
    

    Alternatively, you could do something like this:

    char c = buffer.get(buffertest);
    if ("GE#.".contains(Character.toString(c))) {
        // do something
    }
    

提交回复
热议问题