Java generating Strings with placeholders

前端 未结 8 813
北恋
北恋 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条回答
  •  情歌与酒
    2020-12-15 15:44

    If you can change the format of your placeholder, you could use String.format(). If not, you could also replace it as pre-processing.

    String.format("hello %s!", "world");
    

    More information in this other thread.

提交回复
热议问题