我正在寻找实现以下目标的方法:
String s = "hello {}!";
s = generate(s, new Object[]{ "world" });
assertEquals(s, "hello world!"); // should be true
我可以自己编写它,但在我看来,我曾经见过一个这样做的库,可能是 slf4j 记录器,但我不想编写日志消息。我只想生成字符串。
你知道有这样的图书馆吗?
最佳答案
见 String.format
方法。
String s = "hello %s!";
s = String.format(s, "world");
assertEquals(s, "hello world!"); // should be true
关于Java 使用占位符生成字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17364632/