在 Java 中,实现字符串模板的方式有多种,以下是几种常用的实现方法:
1. String.format
String.format
是 Java 标准库提供的一种格式化字符串的方法。它使用格式化符号来插入变量。
String template = "Hello, %s! You have %d new messages.";
String result = String.format(template, "Alice", 5);
System.out.println(result); // 输出: Hello, Alice! You have 5 new messages.
- 优点:简单直接,适合基本的字符串替换需求。
- 缺点:语法不太直观,格式符号有限制。
2. MessageFormat
MessageFormat
是 Java 标准库中的另一种格式化工具,它支持更加复杂的格式和本地化。
String template = "Hello, {0}! You have {1} new messages.";
String result = MessageFormat.format(template, "Alice", 5);
System.out.println(result); // 输出: Hello, Alice! You have 5 new messages.
- 优点:支持国际化(本地化)和更复杂的格式。
- 缺点:占位符使用
{n}
格式,可能不如%
语法直观。
3. String.replace
如果你的模板相对简单,也可以直接使用 String.replace
或 String.replaceAll
进行占位符替换。
String template = "Hello, ${name}! You have ${count} new messages.";
String result = template.replace("${name}", "Alice").replace("${count}", "5");
System.out.println(result); // 输出: Hello, Alice! You have 5 new messages.
- 优点:直观,灵活性高。
- 缺点:需要手动处理多个替换,可能不够简洁。
4. Apache Commons Text - StringSubstitutor
Apache Commons Text 提供了 StringSubstitutor
,它支持基于占位符的字符串替换,非常灵活。
import org.apache.commons.text.StringSubstitutor;
import java.util.Map;
Map<String, String> valuesMap = Map.of("name", "Alice", "count", "5");
String template = "Hello, ${name}! You have ${count} new messages.";
StringSubstitutor sub = new StringSubstitutor(valuesMap);
String result = sub.replace(template);
System.out.println(result); // 输出: Hello, Alice! You have 5 new messages.
- 优点:非常灵活,支持嵌套、默认值、自定义占位符格式等。
- 缺点:需要引入额外的库依赖。
5. JEXL (Java Expression Language)
JEXL 是一种可以在字符串中嵌入 Java 表达式的库,允许你在模板中使用更复杂的逻辑。
import org.apache.commons.jexl3.*;
import java.util.Map;
JexlEngine jexl = new JexlBuilder().create();
String template = "Hello, ${name}! You have ${count} new messages.";
Map<String, Object> context = Map.of("name", "Alice", "count", 5);
JexlContext jc = new MapContext(context);
String result = jexl.createTemplate(template).evaluate(jc).toString();
System.out.println(result); // 输出: Hello, Alice! You have 5 new messages.
- 优点:支持复杂表达式和逻辑,功能强大。
- 缺点:引入复杂性,通常用于高级需求。
6. Thymeleaf
Thymeleaf 是一种模板引擎,通常用于生成 HTML,但它也可以用来生成任意文本。它支持在模板中插入变量和表达式。
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;
Context context = new Context();
context.setVariable("name", "Alice");
context.setVariable("count", 5);
TemplateEngine templateEngine = new TemplateEngine();
String template = "Hello, [[${name}]]! You have [[${count}]] new messages.";
String result = templateEngine.process(template, context);
System.out.println(result); // 输出: Hello, Alice! You have 5 new messages.
- 优点:强大的模板功能,适合复杂的文本生成场景,特别是 HTML。
- 缺点:相对于简单的文本替换可能有些重,适合更复杂的模板生成需求。
7. Mustache/Handlebars 等模板引擎
Mustache 和 Handlebars 是流行的模板引擎,适用于生成复杂的文本内容。
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile(new StringReader("Hello, {{name}}! You have {{count}} new messages."), "example");
Map<String, Object> context = new HashMap<>();
context.put("name", "Alice");
context.put("count", 5);
StringWriter writer = new StringWriter();
mustache.execute(writer, context).flush();
System.out.println(writer.toString()); // 输出: Hello, Alice! You have 5 new messages.
- 优点:支持逻辑控制、条件判断、循环等复杂功能,非常适合动态内容生成。
- 缺点:需要学习额外的语法,引入了新的依赖。
总结
- 如果你需要简单的占位符替换,可以使用
String.format
、MessageFormat
或String.replace
。 - 如果需要更灵活和复杂的模板处理,可以考虑 Apache Commons Text 的
StringSubstitutor
或模板引擎(如 Thymeleaf 或 Mustache)。 - 如果需要处理复杂的逻辑表达式,可以使用 JEXL 或更复杂的模板引擎。
根据你的需求和项目的复杂性选择合适的方式。