8

Given

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

Task

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

1 Answer 1

14

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.