Search code examples
javastringstring-interpolation

Java generating Strings with placeholders


I'm looking for something to achieve the following:

String s = "hello {}!";
s = generate(s, new Object[]{ "world" });
assertEquals(s, "hello world!"); // should be true

I could write it myself, but It seems to me that I saw a library once which did this, probably it was the slf4j logger, but I don't want to write log messages. I just want to generate strings.

Do you know about a library which does this?


Solution

  • See String.format method.

    String s = "hello %s!";
    s = String.format(s, "world");
    assertEquals(s, "hello world!"); // should be true