Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a string is part of any string in a list of strings with Groovy?

Given

String myString = "blah"
List list = ["a", "b", "cblah", "dblah"]

Task

Check if "blah" is contained in any element of list.

like image 967
Lernkurve Avatar asked Jan 10 '23 02:01

Lernkurve


1 Answers

Solution

println list.any { it.contains(myString) }
println list.find { it.contains(myString) }
list.findAll { it.contains(myString) }.each { print it + " " }

Console output

true
cblah
cblah dblah

Explanation

any returns true or false.

find returns the first element that contains "blah".

findAll returns all elements that contain "blah".

like image 133
Lernkurve Avatar answered Jan 16 '23 20:01

Lernkurve