Ethon

为什么要有方法,因为懒惰是一种美德。

   :: 首页  :: 新随笔  ::  ::  :: 管理

添加依赖

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

import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;

public class test {

    public static void main(String[] args) {

        String name = "张三";
        int age = 16;
        String str1 = "我叫%s,年龄%s";
        String context = String.format(str1, name, age);
        System.out.println("context: " + context);

        System.out.println("-----------------------");

        String st2 = "我叫{0},年龄{1}";
        String context2 = MessageFormat.format(st2, name, age);
        System.out.println("context2: " + context2);

        System.out.println("-----------------------");

        Map<String, String> map = new HashMap<>();
        map.put("name", "张三");
        map.put("age", "16");
        StrSubstitutor strSubstitutor = new StrSubstitutor(map);
        String str3 = "我叫${name},年龄${age}";
        String context3 = strSubstitutor.replace(str3);
        System.out.println("context3: " + context3);
    }
}

 

posted on 2020-09-21 15:08  Ethon  阅读(10116)  评论(0编辑  收藏  举报