java中属性中的动态占位符替换 [英] Dynamic Placeholder substitution in properties in java

查看:27
本文介绍了java中属性中的动态占位符替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Java 应用程序的属性中动态替换占位符.喜欢

I wanted to substitute the placeholder dynamically in properties in a java application. Like

 WelcomeMessage=Welcome Mr. {firstName} {lastName} !!!

这些 firstName 和 LastName 变量需要动态替换.我们应该使用速度模板引擎吗?或者还有其他相同的开源框架吗?

These firstName and LastName variable needs to be substituted dynamically. Should we use velocity template engine for the same? Or are there any other opensource frameworks for the same?

谢谢,曼尼什

推荐答案

您可以使用 MessageFormat 类的 Java SE.它可以让您完全按照您的要求去做.

You can use the MessageFormat class of Java SE. It allows you to do exactly what you ask for.

在您的情况下,以下代码片段必须起作用,假设 props 包含从您的文件加载的所有属性.

In your case the below code snippet must do the trick, assuming props contains all the properties loaded from your file.

MessageFormat.format((String) props.get("WelcomeMessage"), "First", "Last");

请注意,您的属性文件应具有参数索引,而不是如下命名参数.

Note that your properties files should have index of parameters instead of named parameters as below.

WelcomeMessage=Welcome Mr. {0} {1} !!!

这篇关于java中属性中的动态占位符替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆