Groovy not in collection

前端 未结 8 1167
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:24

    The contains(string) method is nice and simple to read if something is contained or not contained in the list to be checked against. Take a look the example below for more insight.

    EXAMPLE

    //Any data you are looking to filter  
    List someData = ['a', 'f', 'b', 'c', 'o', 'o']
    
    //A filter/conditions to not match or match against
    List myFilter = ['a', 'b', 'c'] 
    
    //Some variables for this example
    String filtered, unfiltered 
    
    //Loop the data and filter for not contains or contains
    someData.each {
       if (!myFilter.contains(it)){
          unfiltered += it
       } else {        
          filtered += it
       }
    }
    
    assert unfiltered = "foo"
    assert filtered = "abc"
    

    CONTAINS( )

    Parameters: text - a String to look for.

    Returns: true if this string contains the given text

    source

提交回复
热议问题