voltas
voltas

Reputation: 563

Insert % character in the middle of the string in Groovy

My Code snippet below,(running from Jenkins)

def mainUrl = "http://localhost:8080/job/"
...
jobsName.each(){
  println "Jobs: ${it}"
  println "${mainUrl}${it}/config.xml"
}

Which gives output like below:

Jobs: Env_test
Jobs: Dev_test
Jobs: Model test
Jobs: Prod test

I'm trying to replace the space character with % and used replaceAll method too, still no luck.

println "${mainUrl}${it}.replaceAll("//s","%")/config.xml"

Output I got:

http://localhost:8080/job/Model test.replaceAll(

http://localhost:8080/job/Prod test.replaceAll(

I'm looking for a Output like,

http://localhost:8080/job/Model%test/config.xml
http://localhost:8080/job/Prod%test/config.xml

Any suggestions . Thanks.

Upvotes: 2

Views: 1932

Answers (2)

Valdi_Bo
Valdi_Bo

Reputation: 31011

Change your code to:

println "${mainUrl}${it}".replaceAll("\\s","%") + "/config.xml"

Taking this apart, it means:

  • join mainUrl and it (you missed a double quote char after {it}),
  • replace each space (regex reguires a backslash (not forward slash), but here it should be doubled),
  • and add /config.xml, but as a separate string.

Upvotes: 2

Mike W
Mike W

Reputation: 3932

Try:

println "${mainUrl}${it.replaceAll('\\s','%')}/config.xml" 

Upvotes: 3

Related Questions