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); } }
替换字符串中的某些字符串
最新推荐文章于 2024-02-01 14:07:19 发布