Dynamic Placeholder substitution in properties in java

后端 未结 6 792
死守一世寂寞
死守一世寂寞 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条回答
  •  -上瘾入骨i
    2020-12-01 05:22

    One of the way is string substitutor:

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

    Map valuesMap = new HashMap();
    valuesMap.put("firstName", "ram");
    valuesMap.put("lastName", "Kumar");
    StrSubstitutor sub = new StrSubstitutor(valuesMap);
    String message = sub.replace(WelcomeMessage);
    

提交回复
热议问题