Is there a way to build a Java String using an SLF4J-style formatting function?

后端 未结 3 1534
既然无缘
既然无缘 2021-02-07 03:39

I\'ve heard that using StringBuilder is faster than using string concatenation, but I\'m tired of wrestling with StringBuilder objects all of the time. I was recently exposed t

3条回答
  •  不知归路
    2021-02-07 04:14

    Although the Accepted answer is good, if (like me) one is interested in exactly Slf4J-style semantics, then the correct solution is to use Slf4J's MessageFormatter

    Here is an example usage snippet:

    public static String format(String format, Object... params) {
        return MessageFormatter.arrayFormat(format, params).getMessage();
    }
    

    (Note that this example discards a last argument of type Throwable)

提交回复
热议问题