Search code examples
javapythonstringequivalent

Python string 'Template' equivalent in java


Python supports the following operation:

>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'

(Example taken from Python's official documentation)

Is there an equivalent way in Java for performing the exact task?

Thanks


Solution

  • String s = String.format("%s likes %s", "tim", "kung pao");

    or

    System.out.printf("%s likes %s", "tim", "kung pao");

    you can easily do the templating with this too.

    String s = "%s likes %s";
    String.format(s, "tim", "kung pao");