Apache commons StringSubstitutor 替换占位符
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
Map valuesMap = HashMap();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumped over the ${target}.";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
yielding:
The quick brown fox jumped over the lazy dog.
使用StringSubstitutor替换占位符
本文介绍如何利用Apache Commons Lang3中的StringSubstitutor类实现字符串中占位符的替换。通过构建一个Map对象存储键值对,并定义包含占位符的模板字符串,使用StringSubstitutor完成占位符到实际值的转换。
7728

被折叠的 条评论
为什么被折叠?



