检查一个列表的值包含在一个字符串中? [英] Check a list's value contains in a String?
问题描述
我有一个列表 def vrs = [6.0,6.1,6.1.0]
(versions)此值 def test = [版本:6.1 HotFix11]
。
如何检查测试
的版本
值如果它匹配列表 vrs
?
我试过这个 vrs.each {ver - > println test.version.contains(ver)}
,但它给出了线程main中的异常java.lang.StackOverflowError
更新
原来我的代码有问题。我在一个小小的Groovy脚本中测试了这个测试用例。它工作,所以..
以下是完整的代码
私人地图参数
私人def root
$ b私人def nineBelow
XmlHandler(String xml)
{
nineBelow = [6.0, 6.1,6.1.0]
params = [:]
root = new XmlParser()。parseText(xml)
}
def getParams )
{
if(root.product.version.size()> 0)
{
params.version = root.product.version.text()
}
/*nineBelow.each {
println params.version //甚至会抛出java.lang.StackOverflowError
// println$ it,$ {params.version }
// println ver.getClass()+,+ params.version.getClass()
} * /
println nineBelow.each {ver-> println params.version.contains(ver)}
/ *我需要检查`params.version`是否与`nineBelow`列表匹配,所以我会在这里检查条件* /
params
}
调用 getParams()
static main(args){
String fileContents = new File('E://Projects/agent6.1.xml').text
XmlHandler xm = new XmlHandler(fileContents)
def params = xm.getParams()
println params
}
更新
甚至 println nineBelow.each {println params.version}
给我线程中的异常mainjava .lang.StackOverflowError
更多更新
它只能在下面的代码之后工作
def ver = params.version
println nineBelow.each {println ver.contains (it)}
<
您是否有一个失败的示例,并带有一些示例xml?我问,因为这(你说在你的问题的顶部崩溃)实际工作:
def vrs = [6.0 ,6.1,6.1.0]
def test = [version:'6.1 HotFix11']
vrs.each {ver-> println test.version.contains(ver)}
and print:
false
true
false
但是我找不到其他代码的问题,因为我不知道'E://Projects/agent6.1.xml'
包含...
I have a list def vrs=["6.0","6.1","6.1.0"]
(versions) and I get a map in a function with this value def test=[version:6.1 HotFix11]
.
How can I check test
's version
value if it matches with the list vrs
?
I tried this vrs.each{ver-> println test.version.contains(ver)}
but it gives Exception in thread "main" java.lang.StackOverflowError
Update
Turns out my code has something wrong. I tried the test case in a small groovy script & it works, so..
Here is the full code
private Map params
private def root
private def nineBelow
XmlHandler(String xml)
{
nineBelow=["6.0","6.1","6.1.0"]
params=[:]
root=new XmlParser().parseText(xml)
}
def getParams()
{
if(root.product.version.size()>0)
{
params.version=root.product.version.text()
}
/*nineBelow.each {
println params.version //even this throws java.lang.StackOverflowError
//println "$it , ${params.version}"
//println ver.getClass()+", "+params.version.getClass()
}*/
println nineBelow.each{ver-> println params.version.contains(ver)}
/*I need to check whether `params.version` matches with `nineBelow` list, so i'll check for condition here*/
params
}
Another class which calls getParams()
static main(args) {
String fileContents = new File('E://Projects/agent6.1.xml').text
XmlHandler xm=new XmlHandler(fileContents)
def params=xm.getParams()
println params
}
Update
Even println nineBelow.each { println params.version}
gives me Exception in thread "main" java.lang.StackOverflowError
More Update
It worked only after the below code
def ver=params.version
println nineBelow.each { println ver.contains(it) }
what is the problem here?
Do you have an example that fails with some example xml? I ask because this (which you say at the top of your question crashes) actually works:
def vrs=["6.0","6.1","6.1.0"]
def test=[ version:'6.1 HotFix11' ]
vrs.each { ver-> println test.version.contains( ver ) }
and prints:
false
true
false
But I cannot find a problem with your other code, as I don't know what 'E://Projects/agent6.1.xml'
contains...
这篇关于检查一个列表的值包含在一个字符串中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!