首页 > 编程语言 > 详细

Java字符串占位符(commons-text)替换(转载)

时间:2019-02-10 19:01:45      阅读:299      评论:0      收藏:0      [点我收藏+]

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

https://blog.csdn.net/varyall/article/details/83651798


<dependency>
       <groupId>org.apache.commons</groupId>
       <artifactId>commons-text</artifactId>
       <version>1.6</version>
 </dependency>


@Test
public void test4() {
    String param1 = String.format("hi,%s, your age is %s", "john", "26");
    System.out.println("-------------------param1=" + param1);
    Object[] object = new Object[]{"john", Longs.tryParse("24")};
    MessageFormat messageFormat = new MessageFormat("{0} now is at the age of {1}");
    String param2 = messageFormat.format(object);
    System.out.println("-------------------param2=" + param2);
 
    Map<String, String> replaceValue = Maps.newHashMap();
    replaceValue.put("name", "john");
    replaceValue.put("age", "27");
    StrSubstitutor strSubstitutor = new StrSubstitutor(replaceValue);
    String template1 = "${name} is at the age of${age}";
    String param3 = strSubstitutor.replace(template1);
    System.out.println("-------------------param3=" + param3);
}


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

Java字符串占位符(commons-text)替换(转载)

原文:https://www.cnblogs.com/gispathfinder/p/10359923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!