java中的字符串替换,类似于velocity模板
Java 中是否有任何 String
替换机制,我可以在其中传递带有文本的对象,并在字符串出现时替换它? 例如,文本是:
Hello ${user.name},
Welcome to ${site.name}.
我拥有的对象是 user
和 site
。我想用对象中的等效值替换 ${}
中给出的字符串。这与我们替换 velocity 模板中的对象相同。
Is there any String
replacement mechanism in Java, where I can pass objects with a text, and it replaces the string as it occurs?
For example, the text is:
Hello ${user.name},
Welcome to ${site.name}.
The objects I have are user
and site
. I want to replace the strings given inside ${}
with its equivalent values from the objects. This is same as we replace objects in a velocity template.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用 Apache Commons Text 中的
StringSubstitutor
。依赖项
导入 使用 maven 导入 Apache commons 文本依赖项,如下所示:
示例
Use
StringSubstitutor
from Apache Commons Text.Dependency import
Import the Apache commons text dependency using maven as bellow:
Example
看一下
java.text .MessageFormat
类,MessageFormat 接受一组对象,对它们进行格式化,然后将格式化的字符串插入模式中适当的位置。Take a look at the
java.text.MessageFormat
class, MessageFormat takes a set of objects, formats them, then inserts the formatted strings into the pattern at the appropriate places.我的首选方法是
String.format()
因为它是一个单行代码并且不需要第三方库:我经常使用它,例如在异常消息中,例如:
提示:您可以输入任意多个变量,因为
format()
使用 VarargsMy preferred way is
String.format()
because its a oneliner and doesn't require third party libraries:I use this regularly, e.g. in exception messages like:
Hint: You can put in as many variables as you like because
format()
uses Varargs我对此进行了一个小测试实现。基本思想是调用
format
并传入格式字符串、对象映射以及它们在本地拥有的名称。以下的输出是:
注意:由于未处理的异常,这不会编译。但这使代码更容易阅读。
另外,我不喜欢您必须在代码中自己构建映射,但我不知道如何以编程方式获取局部变量的名称。最好的方法是记住在创建对象后立即将其放入地图中。
以下示例生成您想要的示例结果:
我还应该提到,我不知道 Velocity 是什么,所以我希望这个答案是相关的。
I threw together a small test implementation of this. The basic idea is to call
format
and pass in the format string, and a map of objects, and the names that they have locally.The output of the following is:
Note: This doesn't compile due to unhandled exceptions. But it makes the code much easier to read.
Also, I don't like that you have to construct the map yourself in the code, but I don't know how to get the names of the local variables programatically. The best way to do it, is to remember to put the object in the map as soon as you create it.
The following example produces the results that you want from your example:
I should also mention that I have no idea what Velocity is, so I hope this answer is relevant.
以下概述了如何执行此操作。将其实现为实际代码应该相对简单。
user.name
将变为user
和name
。在地图中查找user
以获取对象并使用 反射从对象中获取name
的值。假设您的对象具有标准 getter,您将查找方法getName
并调用它。Here's an outline of how you could go about doing this. It should be relatively straightforward to implement it as actual code.
user.name
would becomeuser
andname
. Look upuser
in your map to get the object and use reflection to obtain the value ofname
from the object. Assuming your objects have standard getters, you will look for a methodgetName
and invoke it.Java 21 引入了字符串模板作为预览功能。
Java 23 撤回/删除了该功能。
请参阅此处的字符串模板提案 (JEP 430)。
大致是这样的:
PS Kotlin 与 Java 100% 具有互操作性。它支持开箱即用的更清晰的字符串模板:
结合扩展函数,您可以实现相同的效果Java 模板处理器(例如
STR
)将执行的操作。Java 21 introduced string templates as a preview feature.
Java 23 withdrew/removed the feature.
See the string templates proposal (JEP 430) here.
It was something along the lines of this:
P.S. Kotlin is 100% interoperable with Java. It supports cleaner string templates out of the box:
Combined with extension functions, you can achieve the same thing the Java template processors (e.g.
STR
) will do.有一些表达式语言实现可以为您执行此操作,如果您的需求增长,可能比使用您自己的实现更好,请参见例如 JUEL 和 MVEL
我喜欢并已成功使用 MVEL至少一个项目。
另请参阅 Stackflow 帖子 JSTL/JSP EL (非 JSP(独立)上下文中的表达式语言)
There are a couple of Expression Language implementations out there that does this for you, could be preferable to using your own implementation as or if your requirments grow, see for example JUEL and MVEL
I like and have successfully used MVEL in at least one project.
Also see the Stackflow post JSTL/JSP EL (Expression Language) in a non JSP (standalone) context
从 Java 15 开始,您拥有方法
String.formatted()
(请参阅 文档)。str.formatted(args)
相当于String.format(str, args)
,但仪式较少。对于问题中提到的例子,可以使用如下方法:
Since Java 15 you have the method
String.formatted()
(see documentation).str.formatted(args)
is the equivalent ofString.format(str, args)
with less ceremony.For the example mentioned in the question, the method could be used as follows:
就类似 Velocity 的语法和其他服务器端模板功能而言,Handlebars.java 可能是更好的选择。
http://jknack.github.io/handlebars.java/
Handlebars.java might be a better option in terms of a Velocity-like syntax with other server-side templating features.
http://jknack.github.io/handlebars.java/
我在java中使用GroovyShell用Groovy GString解析模板:
I use GroovyShell in java to parse template with Groovy GString:
我创建了这个使用普通 Java 的实用程序。它将 String.format... 中的两种格式...
{}
和%s
样式组合到一个方法调用中。请注意,它仅替换空的{}
括号,而不替换{someWord}
。I created this utility that uses vanilla Java. It combines two formats...
{}
and%s
style from String.format.... into one method call. Please note it only replaces empty{}
brackets, not{someWord}
.没有任何现成的东西可以与速度相媲美,因为速度是为了解决这个问题而编写的。您可以尝试的最接近的方法是查看格式化程序
http://cupi2.uniandes.edu.co/site/images/recursos/javadoc/j2se/1.5.0/docs/api/java/util/Formatter.html
然而格式化程序我知道它的创建是为了在 Java 中提供类似 C 的格式化选项,因此它可能无法完全满足您的需求,但欢迎您尝试:)。
There is nothing out of the box that is comparable to velocity since velocity was written to solve exactly that problem. The closest thing you can try is looking into the Formatter
http://cupi2.uniandes.edu.co/site/images/recursos/javadoc/j2se/1.5.0/docs/api/java/util/Formatter.html
However the formatter as far as I know was created to provide C like formatting options in Java so it may not scratch exactly your itch but you are welcome to try :).