Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if any element of array contains specified string in Groovy

Tags:

groovy

I found this post which describes how to compare two arrays against one another very well. However, if I have some input string given from user like "20394875apple29038475" or "i love apples" and I want to check if any of the strings in a string array are present in the user given string regardless of upper/lower case, how can i do this in groovy?

Let's imagine the string array we are checking to have fruits like ("apple","banana","cherry").

In this case we would return true because the substring "apple" is present in the user given string "20394875apple29038475"

... I am thinking that the best way would be something like this?:

boolean fruitFound = false

for (item in fruitArray){
    if(usrResponse.contains(item)){
        responseFound = true
    }
like image 657
Jonathan Scialpi Avatar asked Dec 10 '22 11:12

Jonathan Scialpi


1 Answers

fruitFound = fruitArray.any{usrResponse.contains(it)}
like image 135
Admiral_x Avatar answered Dec 13 '22 01:12

Admiral_x