在javascript中,我可以编写以下代码:
response = {
status: 1,
data: {
key : 2
}
}
var result = `status is ${response.status}, data key is ${response.data.key}`
console.log(result);
输出为
status is 1, data key is 2
是否有任何库提供在Java中执行以下功能的方法?
String xxxFunction(Map map, String template)
请注意${response.data.key}
在地图中的用法
答案 0 :(得分:2)
您可以使用String.format
String template = "status is %s, data key is %s"
String result = String.format(template, status, key);
答案 1 :(得分:1)
您可以尝试从Apache普通文本中StrSubstitutor
Map valuesMap = HashMap();
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", "lazy dog");
String templateString = "The ${animal} jumps over the ${target}. ${undefined.number:-1234567890}.";
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
您可以找到下载链接/依赖性here