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

 
 <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

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值