Comparing chars in Java

后端 未结 12 526
梦如初夏
梦如初夏 2020-11-29 22:09

I want to check a char variable is one of 21 specific chars, what is the shortest way I can do this?

For example:

if(symbol == (\'A\'|\'B\'|\'C\')){}         


        
12条回答
  •  盖世英雄少女心
    2020-11-29 22:50

    It might be clearer written as a switch statement with fall through e.g.

    switch (symbol){
        case 'A':
        case 'B':
          // Do stuff
          break;
         default:
    }
    

提交回复
热议问题