问题描述
我正在寻找一个很好的模板引擎或一小段代码来在Java中的字符串中扩展类似Ant的变量。示例:
I'm looking for a nice template engine or short piece of code to expand Ant-like variables in a string in Java. Example:
String result = expand ("${firstName} ${familyName}", map);
它应该至少支持 java.util.Map
但也可以在地图/对象列表中处理bean或递归查找或查找的内容。
It should at least support java.util.Map
but something that can handle beans or recursive lookups or lookups in a list of maps/objects would be welcome, too.
建议?
回复TofuBeer:没有嵌套,只有 {}
内的有效Java标识符。应该逐字复制 $ {}
以外的任何内容。 $$
应该变成 $``。如果不可能,$ {dollar}
应该扩展为单个 $
(所以你可以表达 15.00 $
)。
In reply to TofuBeer: No nesting, only valid Java identifiers within the {}
. Anything outside of ${}
should be copied verbatim. $$
should become $``. If that's not possible ${dollar}
should expand to a single $
(so you can express 15.00 $
).
推荐答案
lang /rel =nofollow noreferrer> Commons Lang 几乎可以满足您的要求
StrSubstitutor from Commons Lang does pretty much what you're asking for
这篇关于替换文本中的变量:建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!