当前位置:   article > 正文

使用Java替换字符串中的占位符${xx}_java根据${}替换

java根据${}替换

引入pom依赖

<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5

使用

import org.apache.commons.lang.text.StrSubstitutor;
import java.util.HashMap;
import java.util.Map;

public class test {
    public static void main(String[] args) {
        Map<String, Object> map = new HashMap<>();
        map.put("name", "旺旺");
        map.put("gender", "未知");
        String s = "双重否定的${name}, 性别是${gender}";
        System.out.println(replaceAll(map, s));
    }
    // 替换所有占位符
    public static String replaceAll(Map<String, Object> map, String replaceValue){
        StrSubstitutor strSubstitutor = new StrSubstitutor(map);
        return strSubstitutor.replace(replaceValue);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

运行结果

双重否定的旺旺, 性别是未知
  • 1
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/829759
推荐阅读
相关标签
  

闽ICP备14008679号