Dynamic Placeholder substitution in properties in java

后端 未结 6 791
死守一世寂寞
死守一世寂寞 2020-12-01 04:49

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

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


        
6条回答
  •  离开以前
    2020-12-01 05:24

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

    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} !!!
    

提交回复
热议问题