string Groovy:如何检查字符串是否包含数组的任何元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27069701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:19:20 来源:igfitidea点击:
Groovy: How to check if a string contains any element of an array?
提问by User001
I have an array of strings pointAddress
and I want to check each entry if it contains strings from another array, validPointTypes
.
我有一个字符串数组,pointAddress
我想检查每个条目是否包含来自另一个数组的字符串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.
现在我只有一个巨大的if
声明。
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?
一定有更好的方法,对吧?
回答by tim_yates
def valid = pointAddress.findAll { a ->
validPointTypes.any { a.contains(it) }
}
Should do it
应该做