Search code examples
stringgroovystring-formatting

Format string in groovy


I would like to substitute %s with the value

<server>
    <id>artifactory</id>
    <username>%s</username>
    <password>%s</password>
</server>

Is there any myString.format("name", "pass") method in groovy?


Solution

  • 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")