apache commons-lang3字符串替换方法StrSubstitutor过期

雷国兴
2023-12-01

先来看看StrSubstitutor的用法

Map<String,String> valuesMap = new HashMap();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumps over the ${target}.";
 StrSubstitutor sub = new StrSubstitutor(valuesMap);
 String resolvedString = sub.replace(templateString);

org.apache.commons commons-lang3在 3.6以后废弃了该方法,apache建议替换成 commons-text 包中的StringSubstitutor
官方javadoc说明:
https://commons.apache.org/proper/commons-lang/javadocs/api-release/index.html

使用StringSubstitutor需要添加apache commons-text依赖

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

使用方法与StrSubstitutor 一样

 Map<String,String> valuesMap = new HashMap();
 valuesMap.put("animal", "quick brown fox");
 valuesMap.put("target", "lazy dog");
 String templateString = "The ${animal} jumped over the ${target}.";
 StringSubstitutor sub = new StringSubstitutor(valuesMap);
 String resolvedString = sub.replace(templateString);

更多功能可以查看官方javadoc
https://commons.apache.org/proper/commons-text/javadocs/api-release/org/apache/commons/text/StringSubstitutor.html

 类似资料: