当前位置:   article > 正文

Java字符串占位符(commons-text)替换_commons text 文本替换

commons text 文本替换
 
  1. <dependency>
  2. <groupId>org.apache.commons</groupId>
  3. <artifactId>commons-text</artifactId>
  4. <version>1.6</version>
  5. </dependency>
  1. @Test
  2. public void test4() {
  3. String param1 = String.format("hi,%s, your age is %s", "john", "26");
  4. System.out.println("-------------------param1=" + param1);
  5. Object[] object = new Object[]{"john", Longs.tryParse("24")};
  6. MessageFormat messageFormat = new MessageFormat("{0} now is at the age of {1}");
  7. String param2 = messageFormat.format(object);
  8. System.out.println("-------------------param2=" + param2);
  9. Map<String, String> replaceValue = Maps.newHashMap();
  10. replaceValue.put("name", "john");
  11. replaceValue.put("age", "27");
  12. StrSubstitutor strSubstitutor = new StrSubstitutor(replaceValue);
  13. String template1 = "${name} is at the age of${age}";
  14. String param3 = strSubstitutor.replace(template1);
  15. System.out.println("-------------------param3=" + param3);
  16. }


-------------------param1=hi,john, your age is 26
-------------------param2=john now is at the age of 24
-------------------param3=john is at the age of27

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/817054
推荐阅读
相关标签
  

闽ICP备14008679号