java通过ST4使用模板字符串

java通过ST4使用模板字符串

目前为止,java官方好像都没有类似 ES6或者 python 3.6 里面的模板字符串。

我个人觉得使用StringBuilder拼字符串是比较麻烦的,并且不美观。终于,我找到了一个第三方的库ST4

ST4的使用

pom文件里面添加依赖

<dependency>
    <groupId>org.antlr</groupId>
    <artifactId>ST4</artifactId>
    <version>4.3</version>
    <scope>compile</scope>
</dependency>

编写代码

@Test
void testTemplateString1() {
    ST hello = new ST("Hello, <name>!");
    hello.add("name", "World");
    String output = hello.render();
    System.out.println(output);
}

输出的结果

image-20210502113602627

参考信息

Github项目地址: antlr/stringtemplate4: StringTemplate 4 (github.com)

官网: StringTemplate

posted @ 2021-05-02 11:42  Blithe-Chiang  阅读(511)  评论(0编辑  收藏  举报