当前位置:   article > 正文

JAVA文本占位符替换_java 动态 替换 yml 文件占位符

java 动态 替换 yml 文件占位符


需求

一、JAVA文本占位符替换

我的代码估计可以解决你所问的问题
首先占位符在文本必须统一 都是问号或者其他

例如:String content = "你好: ?\n恭喜你试工成功,你的临时工号为:?。"

我的需求是发短信,里面有换行符
我这里是封装的很好了,你可以根据你的需求而定

1.代码实战

(1).模板枚举


/**
 * @ProductName: IntelliJ IDEA
 * @ProjectName: tangerine_util
 * @Description:
 * @Witticism: Your ability to learn and experience in solving problems will always be the only thing holding you back
 * @User: Tangerine
 * @Email: yj_tangerine@163.com | yj_tangerine@sina.com
 * @Date: 2021/9/13 21:42 星期一
 * @version: V1.0.0
 */
public class Enums {
    public enum SmsTemplate {
		VIEW("你好: ?\n  恭喜你试工成功,你的临时工号为:?。", 2),
        TEST("我是&,你是&,他是&.", 3);
        String content;
        Integer paramSize;

        SmsTemplate(String content, Integer paramSize) {
            this.content = content;
            this.paramSize = paramSize;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public Integer getParamSize() {
            return paramSize;
        }

        public void setParamSize(Integer paramSize) {
            this.paramSize = paramSize;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

(2).封装工具


import com.tangerine.enums.Enums;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
 * @ProductName: IntelliJ IDEA
 * @ProjectName: hr-homa-service
 * @Description: 短信内容工具 替换模板参数....等
 * @Witticism: Your ability to learn and experience in solving problems will always be the only thing holding you back
 * @User: Tangerine
 * @Email: yj_tangerine@163.com | yj_tangerine@sina.com
 * @Date: 2021/8/24 15:08 星期二
 * @version: V1.0.0
 */
public class SmsContentUtil {
    /**默认替换问号占位符*/
    public static final String replace(Enums.SmsTemplate template, List<String> params) {
        return replace(template, "?", params);
    }
	/** 自己传递占位符*/
    public static final String replace(Enums.SmsTemplate template, String symbol, List<String> params) {
        /**
         * @Description: 将模板中的短信内容字符串里的{symbol}替换为 params
         * @Auther: tangerine
         * @Email: yj_tangerine@163.com and yj_tangerine@sina.com
         * @Date: 2021/8/25 0:55
         * @param template 短信模板枚举类
         * @param symbol 占位符
         * @param params 内部因有remove操作,所以避免Arrays.asList()操作
         * @return: java.lang.String
         * @Exception: RuntimeException 如果参数和模板对应的参数数量不匹配,抛出异常
         */
        return execute(template.getContent(), template.getParamSize(), (ct, ps) -> {
            if (!(ps == params.size()))
                throw new RuntimeException("The number of parameters does not match");
            return Arrays.asList(ct.split("")).stream().map(e -> e.equals(symbol) ? params.remove(0) : e).collect(Collectors.joining());
        });
    }
    private static final String execute(String content, Integer paramSize, BiFunction<String, Integer, String> bf) {
        return bf.apply(content, paramSize);
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

(3).使用教程

不看注释的程序员,不一定是一个坏的程序员,但肯定不是一个好的程序员,,,,,,哈哈哈

    @Test
    public void testSmsTemplate(){
        //一定要看好我的注释
        System.out.println("------------------不传递占位符,默认替换问号--------------------");
        String replace1 = SmsContentUtil.replace(Enums.SmsTemplate.VIEW, new ArrayList<String>() {{
            //我为了简单,写的有点low
            add("欧阳文彦");
            add("100001");
        }});
        System.out.println(replace1);
        System.out.println("------------------传递占位符,指定替换占位符--------------------");
        String replace2 = SmsContentUtil.replace(Enums.SmsTemplate.TEST, "&", new ArrayList<String>() {{
            //我为了简单,写的有点low
            add("欧阳文彦");
            add("欧阳娜娜");
            add("欧阳雨涵");
        }});
        System.out.println(replace2);

        //1.SmsContentUtil.replace 这个方法有重载,如果不传递替换的符号,那么会自动替换问号(?),
        //2.枚举里面的paramSize必须要和传进去的list.size相等,不一致肯定出问题
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

(4).输出结果

------------------不传递占位符,默认替换问号--------------------
你好: 欧阳文彦
  恭喜你试工成功,你的临时工号为:100001------------------传递占位符,指定替换占位符--------------------
我是欧阳文彦,你是欧阳娜娜,他是欧阳雨涵.
  • 1
  • 2
  • 3
  • 4
  • 5
本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号