[英]if an attribute is not present in map, How to replace the values in template string using strsubsitutor as null or empty string
I have a string我有一个字符串
String templateString = "The ${animal} jumps over the ${target}.";
valuesMap.put("animal", "quick brown fox");
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
But there is no entry for attr target in valuesMap.但是 valuesMap 中没有 attr target 的条目。 Final resolvedString would be The quick brown fox jumps over the ${target}.最终的 resolveString 将是 The quick brown fox jumps over ${target}。
Instead of ${target}, it need to be empty.而不是 ${target},它需要为空。 Values in templatestring which doesn't have key in map should be empty or null.在映射中没有键的模板字符串中的值应该为空或 null。
required The quick brown fox jumps over the.需要 敏捷的棕色狐狸跳过了。
How to handle this如何处理这个
Your Map<String,String> valuesMap
contains just the couple key, value "animal", "quick brown fox"
, you have to add the couple "target", ""
to your map like below:您的Map<String,String> valuesMap
仅包含一对键值"animal", "quick brown fox"
,您必须将一对"target", ""
到您的地图中,如下所示:
String templateString = "The ${animal} jumps over the ${target}.";
valuesMap.put("animal", "quick brown fox");
valuesMap.put("target", ""); //<-- adding the new couple to the map
StrSubstitutor sub = new StrSubstitutor(valuesMap);
String resolvedString = sub.replace(templateString);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.