Java 占位符替换
源码:
https://github.com/caojx-git/java-utils/blob/master/src/main/java/personal/caojx/placeholder/PlaceholderUtil.java
package personal.caojx.placeholder;
import org.apache.commons.text.StringSubstitutor;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
public class PlaceholderUtil {
public static void main(String[] args) {
String templateResult1 = String.format("%s is at the age of %s", "john", "26");
System.out.println(templateResult1);
Object[] object = new Object[]{"john", "24"};
MessageFormat messageFormat = new MessageFormat("{0} is at the age of {1}");
String templateResult2 = messageFormat.format(object);
System.out.println(templateResult2);
Map<String, String> paramMap = new HashMap<>();
paramMap.put("name", "john");
paramMap.put("age", "27");
StringSubstitutor stringSubstitutor = new StringSubstitutor(paramMap);
String template3 = "${name} is at the age of ${age}";
String templateResult3 = stringSubstitutor.replace(template3);
System.out.println(templateResult3);
}
}