[arrays] Groovy: How to check if a string contains any element of an array?

I have an array of strings pointAddress and I want to check each entry if it contains strings from another array, validPointTypes.

def pointAddress = ['bacnet://240101/AV:3', 'bacnet://240101/BV:9', 'bacnet://240101/AV:7', 'bacnet://240101/BALM:15']
def validPointTypes = ['AV', 'AI', 'AO', 'ANI', 'ANO', 'BV', 'BI', 'BO', 'BNI', 'BNO']

Right now I just have a giant if statement.

j = pointName.size()
for(j=j-1; j>=0;j--) {
    if(pointAddress[j]) {
        if(pointAddress[j].contains('AV') || pointAddress[j].contains('AI') || 
            pointAddress[j].contains('AO') || pointAddress[j].contains('ANI') || 
            pointAddress[j].contains('ANO') || pointAddress[j].contains('BV') || 
            pointAddress[j].contains('BI') || pointAddress[j].contains('BO') || 
            pointAddress[j].contains('BNI') || pointAddress[j].contains('BNO')) {
        } else {
            pointAddress.remove(j)
            pointName.remove(j)
            m++
        }
    } else {
        pointName.remove(j)
        m++
    }
}

There's gotta be a better way, right?

This question is related to arrays string groovy

The answer is


def valid = pointAddress.findAll { a ->
    validPointTypes.any { a.contains(it) }
}

Should do it


Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to string

How to split a string in two and store it in a field String method cannot be found in a main class method Kotlin - How to correctly concatenate a String Replacing a character from a certain index Remove quotes from String in Python Detect whether a Python string is a number or a letter How does String substring work in Swift How does String.Index work in Swift swift 3.0 Data to String? How to parse JSON string in Typescript

Examples related to groovy

Jenkins pipeline how to change to another folder groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding Run bash command on jenkins pipeline Try-catch block in Jenkins pipeline script How to print a Groovy variable in Jenkins? Jenkins pipeline if else not working How to set and reference a variable in a Jenkinsfile Jenkins: Can comments be added to a Jenkinsfile? How to define and use function inside Jenkins Pipeline config? Jenkins: Cannot define variable in pipeline stage