[英]Groovy check array contains string does not work identically with literal string and concat string
When you check a literal string within a list or array, it works well. 当您检查列表或数组中的文字字符串时,它会很好地工作。 But when you check a concat string within a list or array, it returns incorrect result. 但是,当您检查列表或数组中的concat字符串时,它将返回错误的结果。
And when compare the literal string and concat string directly, they are considered identical. 并且当直接比较文字字符串和concat字符串时,它们被认为是相同的。
I failed at Jenkins pipeline with these code and I was tried on Groovy 1.8.9 directly, the result was the same. 我在Jenkins管道中使用这些代码失败,并且直接在Groovy 1.8.9上进行了尝试,结果是相同的。
In this code, issue_file = "issue-${signal_dc_key}.json"
, then contains
returns false
在此代码中, issue_file = "issue-${signal_dc_key}.json"
,然后contains
返回false
def issued_list = ["names":["issue-something-DC-VV.json","issue-else-DC-VV.json"]]
def signal_dc_key = "something-DC-VV"
def issue_file = "issue-${signal_dc_key}.json"
println issue_file // issue-something-DC-VV.json
println issue_file == "issue-something-DC-VV.json" // true
println issue_file == "issue-${signal_dc_key}.json" // true
println issued_list["names"].contains("${issue_file}") // false
println issued_list["names"].contains(issue_file) // false
println issued_list["names"].contains("issue-something-DC-VV.json") // true
In this code, issue_file = "issue-something-DC-VV.json"
, contains
returns true
. 在此代码中, issue_file = "issue-something-DC-VV.json"
contains
返回true
。
def issued_list = ["names":["issue-something-DC-VV.json","issue-else-DC-VV.json"]]
def signal_dc_key = "something-DC-VV"
def issue_file = "issue-something-DC-VV.json"
println issue_file // issue-something-DC-VV.json
println issue_file == "issue-something-DC-VV.json" // true
println issue_file == "issue-${signal_dc_key}.json" // true
println issued_list["names"].contains("${issue_file}") // false
println issued_list["names"].contains(issue_file) // true
println issued_list["names"].contains("issue-something-DC-VV.json") // true
It seems like got fixed by changing this line 似乎通过更改此行已解决
def issue_file = "issue-${signal_dc_key}.json"
to 至
def issue_file = new String("issue-${signal_dc_key}.json" )
But new String
is NOT permitted in Jenkins. 但是在詹金斯中不允许使用new String
。
Thanks to Michael Rutherfurd's comment, toString()
works and is permitted in Jenkins. 感谢Michael Rutherfurd的评论,Jenkins允许使用toString()
。
def issue_file = "issue-${signal_dc_key}.json".toString()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.