How to check if a character is correct

前端 未结 4 1923
情话喂你
情话喂你 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:32

    You haven't shown the type of buffer, which makes things harder. But assuming buffer.get returns a char, you could use:

    if ("GE#.".indexOf(buffer.get(buffertest) >= 0)
    

    Or you could check each option explicitly, as per Simulant's answer... or to do the same thing but only calling get once:

    char x = buffer.get(buffertest);
    if (x == 'G' || x == 'E' || x == '#' || x == '.')
    

    Your original code is failing because | is trying to perform a bitwise "OR" operation on the four characters... it's not the same thing as performing a logical "OR" on four conditions.

提交回复
热议问题