Format string in groovy

You can use sprintf from DefaultGroovyMethods

def name = "name"
def pass = "pass"

String formatted = """
<server>
    <id>artifactory</id>
    <username>$name</username>
    <password>$pass</password>
</server>
"""
def f = sprintf( formatted, name, pass )

Groovy has built-in support for string interpolation. All you need is to use a GString:

def name = "name"
def pass = "pass"

String formatted = """
<server>
    <id>artifactory</id>
    <username>$name</username>
    <password>$pass</password>
</server>
"""

If your values come as an array or collection, you can even use params[n] instead of named variables ($name), like this:

def params = ['name', 'pass']

String formatted = """
<server>
    <id>artifactory</id>
    <username>${params[0]}</username>
    <password>${params[1]}</password>
</server>
"""

If your string needs to be externalized, you can use template engines

Beside this, you can use the normal Java String.format:

def formatted = String.format(myString, "name", "pass")

groovy based on java and in java there is a format method in String class

https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#format(java.lang.String,%20java.lang.Object...)

so this should work

def s='''<server>
    <id>artifactory</id>
    <username>%s</username>
    <password>%s</password>
</server>'''
println String.format(s, "name", "pass")