简体   繁体   English

如何为字符串构建 URL 并添加到 groovy 中的列表

[英]How to build URL for string and add to list in groovy

I have a list like below ticketList and need a ResultList as below with a hyperlink attached我有一个类似于以下ticketList 的列表,需要一个如下所示的ResultList,并附有一个超链接

ticketList = [JR8908,JR7676,JR7687,JR8798]票列表 = [JR8908,JR7676,JR7687,JR8798]

I want the result ResultList: [ JR8908 , JR7676 , JR7687 , JR8798 ]我想要结果 ResultList: [ JR8908 , JR7676 , JR7687 , JR8798 ]

Hyperlink would be - http://localhost:8080/browse/ticketID超链接将是 - http://localhost:8080/browse/ticketID

End point will change for each ticket, it will be ticket ID每张票的终点都会改变,它将是票号

Tried Code:试过的代码:

ticketList = ["JR8908","JR7676","JR7687","JR8798"]
finalList = [] 

ticketList.eachWithIndex { ticket, index ->
            println ticket
    replacedstring = ticket.replaceAll("$ticket", "<a href='http://localhost:8080/browse/$ticket'>$ticket</a>")
        finalList.add(replacedstring)
}
        println finalList

finalist result is below:决赛结果如下:

[<a href='http://localhost:8080/browse/JR8908'>JR8908</a>, <a href='http://localhost:8080/browse/JR7676'>JR7676</a>, <a href='http://localhost:8080/browse/JR7687'>JR7687</a>, <a href='http://localhost:8080/browse/JR8798'>JR8798</a>]

But,I want it like: [ JR8908 , JR7676 , JR7687 , JR8798 ]但是,我想要它:[ JR8908JR7676JR7687JR8798 ]

Use collect with String interpolation:将 collect 与字符串插值一起使用:

def ticketList = ["JR8908","JR7676","JR7687","JR8798"]

def finalList = ticketList.collect{ "<a href='http://localhost:8080/browse/$it'>$it</a>" }

assert finalList.toString() == '[<a href='http://localhost:8080/browse/JR8908'>JR8908</a>, <a href='http://localhost:8080/browse/JR7676'>JR7676</a>, <a href='http://localhost:8080/browse/JR7687'>JR7687</a>, <a href='http://localhost:8080/browse/JR8798'>JR8798</a>]' 

It looks like you want the output to be strings but your code is printing a list.看起来您希望 output 是字符串,但您的代码正在打印一个列表。 You will have to iterate over the list:您将不得不遍历列表:

finalList.each { println it }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM