Groovy没有收藏 [英] Groovy not in collection
本文介绍了Groovy没有收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
if('b'in ['a','b','c'])
然而,您如何很好地看到有些东西不在集合中?
if(!('''in''a','b','c ']))
似乎凌乱和!隐藏在随便的一瞥中。有没有更习惯性的常规方式来做到这一点?
谢谢!
解决方案
不幸的是,我认为没有不在
漂亮的语法中。但是你可以使用一个辅助变量来提高它的可读性:
def isMagicChar = ch in ['a','b' ,'c']
if(!isMagicChar)...
例如,您可以使用正则表达式:)
if(ch!=〜/ [abc] /)...
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 is not in a collection?
if(!('g' in ['a','b','c']))
Seems messy and the "!" is hidden to the casual glance. Is there a more idiomatic groovy way to do this?
Thanks!
解决方案
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]/) ...
这篇关于Groovy没有收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文