Groovy not in collection

前端 未结 8 1160
Happy的楠姐
Happy的楠姐 2021-02-04 23:50

The groovy way to see if something is in a list is to use \"in\"

   if(\'b\' in [\'a\',\'b\',\'c\'])

However how do you nicely see if something

8条回答
  •  别跟我提以往
    2021-02-05 00:29

    I think there is no not in pretty syntax, unfortunately. But you can use a helper variable to make it more readable:

    def isMagicChar = ch in ['a', 'b', 'c']
    if (!isMagicChar) ...
    

    Or, in this case, you may use a regex :)

    if (ch !=~ /[abc]/) ...
    

提交回复
热议问题