String replacement in java, similar to a velocity template

前端 未结 8 1375
小鲜肉
小鲜肉 2020-11-28 04:59

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 :

8条回答
  •  生来不讨喜
    2020-11-28 05:29

    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.

    Object[] params = new Object[]{"hello", "!"};
    String msg = MessageFormat.format("{0} world {1}", params);
    

提交回复
热议问题