三种操作字符串替换的方法

1、传统的字符串替换方式

public class TestMain {

    // 短信模板
    private static final String messageTemp = "【XXX】您的验证码为${验证码},有效时间为${有效期}分钟。";

    private static final String verificationCode = "\\$\\{验证码}";
    private static final String effectiveTime = "\\$\\{有效期}";

    public static void main(String[] args) {
        String message = messageTemp.replaceAll(verificationCode, "123456").replaceAll(effectiveTime, "3");
        System.out.println("发送的短信为:" + message);
    }
}
发送的短信为:【XXX】您的验证码为:123456,有效时间为:3分钟。

2、PropertyPlaceholderHelper占位符的字符串替换方式(推荐)

public class TestMain {

    // 短信模板
    private static final String messageTemp = "【XXX】您的验证码为${验证码},有效时间为${有效期}分钟。";

    private static final String verificationCode = "验证码";
    private static final String effectiveTime = "有效期";

    private static final String PLACE_HOLDER_PREFIX = "${";
    private static final String PLACE_HOLDER_SUFFIX = "}";
    private static final PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper(PLACE_HOLDER_PREFIX, PLACE_HOLDER_SUFFIX);

    /**
     * 获取短信,执行替换
     *
     * @param placeHolder
     * @return
     */
    public static String replace(Properties placeHolder) {
        return placeholderHelper.replacePlaceholders(messageTemp, placeHolder);
    }

    /**
     * 初始化Properties,将要替换的参数一一对应
     *
     * @param verCode 真实验证码  如:123456
     * @param effTime 真实有效期  如:3
     * @return
     */
    public static Properties initProperties(String verCode, String effTime) {
        Properties placeHolder = new Properties();
        placeHolder.put(verificationCode, verCode);
        placeHolder.put(effectiveTime, effTime);
        return placeHolder;
    }


    public static void main(String[] args) {
        Properties properties = initProperties("123456", "3");
        String message = replace(properties);
        System.out.println("发送的短信为:" + message);
    }
}
发送的短信为:【XXX】您的验证码为:123456,有效时间为:3分钟。

3、根据替换结果获取参数列表

如:接收到短信发送内容为:【XXX】您的验证码为:123456,有效时间为:3分钟。
但是短信平台接收的参数为列表:["123456", "3"],接下来的内容为反向获取参数列表

public class TestMain {

    // 短信模板
    private static final CharSequence messageTemp = "【XXX】您的验证码为:${验证码},有效时间为:${有效期}分钟。";

    // 获取参数
    private static List<String> getParams(String msg) {
        String regex = messageTemp.toString();
        Pattern pattern = Pattern.compile("\\$\\{(.*?)}");
        Matcher matcher = pattern.matcher(messageTemp);
        while (matcher.find()) {
            regex = regex.replace(matcher.group(0), "(.*?)");
        }
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(msg);
        List<String> result = new ArrayList<>();
        if (matcher.find()) {
            for (int i = 1; i <= matcher.groupCount(); i++) {
                result.add(matcher.group(i));
            }
        } else {
            System.out.println("请检查短信模板是否修改");
        }
        return result;
    }

    public static void main(String[] args) {
        String msg = "【XXX】您的验证码为:123456,有效时间为:3分钟。";
        List<String> params = getParams(msg);
        for (String param : params) {
            System.out.println(param);
        }
    }
}
posted @ 2024-04-19 17:21  Dyaqi  阅读(33)  评论(0编辑  收藏  举报