掘金 后端 ( ) • 2024-04-12 13:45

阅读说明:

  1. 如果有排版格式问题,请移步https://www.yuque.com/mrhuang-ire4d/oufb8x/xt5vqiawpx2iqce2?singleDoc# 《java占位符替换五种方式》,选择宽屏模式效果更佳。
  2. 本文为原创文章,转发请注明出处。如果觉得文章不错,请点赞、收藏、关注一下,您的认可是我写作的动力。

简介

在业务开发中,经常需要输出文本串。其中,部分场景下大部分文本内容是一样的,只有少部分点是不一样。简单做法是直接拼接字段,但是这会有个问题。后面如果想要修改内容,那么每个地方都要修改,这不符合设计模式中的开闭原则,面向应该对扩展开放,对修改关闭。

如何解决这个问题? 先来看现实生活中的例子,个人信息填写。一般会要填写表格,已经定义好了姓名,性别,年龄等字段,只要填写对应的个人信息就好了。在程序开发中,我们会预先定义好一个字符串模板,需要改动的点使用占位符。在使用中,使用具体的内容替换掉占位符,就能生成我们所需要的文本内容。

本文总结了占位符替换五种方法。前三种方法占位符格式支持自定义。本文使用${param!}的格式,前三种方法示例模板为:

String TEMPLATE = "姓名:${name!},年龄:${age!},手机号:${phone!}";

待填充参数为:

private static final Map<String, String> ARGS = new HashMap<String, String>() {{
        put("name", "张三");
        put("phone", "10086");
        put("age", "21");
    }};

StringUtils.replace

StringUtils.replace方法定义:

String replace(String text, String searchString, String replacement);

StringUtils.replace能够将入参中的searchString字符串替换成replacement字符串。

示例:

String remark = TEMPLATE;
for (Map.Entry<String, String> entry : ARGS.entrySet()) {
    remark = StringUtils.replace(remark, "${" + entry.getKey() + "!}", entry.getValue());
}
System.out.println(remark);

正则表达式Matcher

使用正则表达式依次找到占位符的位置,使用实际参数替换掉占位符。

示例:

Pattern TEMPLATE_ARG_PATTERN = Pattern.compile("\\$\\{(.+?)!}"); // ${param!}
Matcher m = TEMPLATE_ARG_PATTERN.matcher(TEMPLATE);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    String arg = m.group(1);
    String replaceStr = ARGS.get(arg);
    m.appendReplacement(sb, replaceStr != null ? replaceStr : "");
}
m.appendTail(sb);
System.out.println(sb.toString());

StringSubstitutor

StringSubstitutor功能比较多,使用起来比较灵活。支持默认占位符${param}, 当然也可以自定义前缀和后缀,甚至支持默认值。

public <V> StringSubstitutor(Map<String, V> valueMap);
public <V> StringSubstitutor(Map<String, V> valueMap, String prefix, String suffix);

示例:

StringSubstitutor sub = new StringSubstitutor(ARGS,"${","!}");
System.out.println(sub.replace(TEMPLATE));

MessageFormat.format

MessageFormat也能使用,但不是太灵活,占位符格式为${param}。

String template = "Hello, my name is ${name} and I am ${age} years old.";
Map<String, Object> values = new HashMap<>();
values.put("name", "Alice");
values.put("age", 25);

StringSubstitutor sub = new StringSubstitutor(values);
String message = sub.replace(template);
System.out.println(message); // 输出:Hello, my name is Alice and I am 25 years old.

String.format

String.format也能实现,同样因为不灵活不建议使用。占位符名称必须使用 () 进行括起来,并在名称前面加上 % 符号,例如:%(age)d。

String template = "Hello, my name is %(name)s and I am %(age)d years old.";
Map<String, Object> values = new HashMap<>();
values.put("name", "Alice");
values.put("age", 25);

String message = String.format(template, values);
System.out.println(message); // 输出:Hello, my name is Alice and I am 25 years old.

参考: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/text/StrSubstitutor.html https://developer.aliyun.com/article/1257908