替换字符串中的某些字符串

public class StringReplace {

    public static String replace(String templete,String[] param){
        if(param == null || param.length == 0){
            return templete;
        }
        int size = param.length;
        for(int i = 0; i < size; i++){
            int start = templete.indexOf("[");
            if(start == -1){
                return templete;
            }
            int end = templete.indexOf("]");
            String temp = templete.substring(start,end + 1);
            templete = templete.replace(temp,param[i]);
        }
        return templete;
    }


    public static String replace(String templete,Map<String, String> properties){
        if(properties == null || properties.isEmpty()){
            return templete;
        }
        for (Map.Entry<String, String> entry : properties.entrySet()){
            String key = entry.getKey();
            String value = entry.getValue();
            key = "[" + key + "]";
            templete = templete.replace(key,value);
        }
        return templete;
    }
    /**
     * 适用于替换中文【】,并且含有相同替换内容的模板
     */
    public static String replaceZ_CH(String templete,String[] param){
        if(param == null || param.length == 0){
            return templete;
        }
        int size = param.length;
        for(int i = 0; i < size; i++){
            int start = templete.indexOf("【");
            if(start == -1){
                return templete;
            }
            int end = templete.indexOf("】");
            String temp = templete.substring(start,end + 1);
            templete = templete.replace(templete.substring(0,end+1),templete.substring(0,end+1).replace(temp,param[i]));
        }
        return templete;
    }

    public static void main(String[] args) {
        Map<String, String> properties = new HashMap<>();
        properties.put("name1","王");
        properties.put("name2","李");
        properties.put("name3","张");
        properties.put("name4","高");
        String result = replace("[name1]的[name2]的主管医生由[name3]变更为[name4]的申请未通过",properties);
        System.out.println(result);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值