Syed
Syed

Reputation: 2597

Find a string in a list using groovy collection

I'm trying to find a string in a list using Groovy

Here is my working code:

for(Export e : lstexport) {
    if(e.keyValue.contains("mountain")) {
        return "mountain";
    }
return ""

Not working code

String str = lstexport.collect{it.keyValue}.contains("mountain").toString() ? "mountain" : ""

This is always returning value as false.

Upvotes: 0

Views: 1181

Answers (1)

Michal M
Michal M

Reputation: 1568

Use find:

println (['a','b','c'].find{ it == 'c' }​ ?: 'not found')​

Try it at https://groovyconsole.appspot.com/script/5146305110212608 .

Upvotes: 2

Related Questions