检查列表的值是否包含在字符串中 [英] Check if a list's value contains in a string

查看:62
本文介绍了检查列表的值是否包含在字符串中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表 def vrs = ["6.0","6.1","6.1.0"] (版本),并且在函数中获得了一个具有此值 def的映射test = [version:6.1 HotFix11] .

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

如何检查 test version 值是否与列表 vrs 相匹配?

How can I check whether test's version value matches with the list vrs?

我尝试了这个 vrs.each {ver->println test.version.contains(ver)} ,但它在线程"main" java.lang.StackOverflowError

I tried this vrs.each{ver-> println test.version.contains(ver)} but it gives Exception in thread "main" java.lang.StackOverflowError

更新

结果是,我的代码出了点问题.我在一个小的Groovy脚本中尝试了该测试用例,它可以工作.

Turns out, there was something wrong with my code. I tried the test case in a small Groovy script and it works.

这是完整的代码:

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
}

另一个调用 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} 给我线程"main"中的异常java.lang.StackOverflowError

更多更新

仅在以下代码之后有效

def ver=params.version
    println nineBelow.each { println ver.contains(it) }

这是什么问题?

推荐答案

您是否有一个示例失败,并带有一些示例xml?我问,因为这(您在问题顶部说的是崩溃)实际上有效:

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 ) }

并打印:

false
true
false

但是我找不到您的其他代码的问题,因为我不知道'E://Projects/agent6.1.xml'包含的内容...

But I cannot find a problem with your other code, as I don't know what 'E://Projects/agent6.1.xml' contains...

这篇关于检查列表的值是否包含在字符串中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆