Java generating Strings with placeholders

前端 未结 8 810
北恋
北恋 2020-12-15 14:46

I\'m looking for something to achieve the following:

String s = \"hello {}!\";
s = generate(s, new Object[]{ \"world\" });
assertEquals(s, \"hello world!\");         


        
8条回答
  •  -上瘾入骨i
    2020-12-15 15:34

    See String.format method.

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

提交回复
热议问题