StrSubstitutor用法

本文介绍了一个使用Java的StrSubstitutor类来实现模板字符串中变量替换的示例。通过创建一个包含变量值的Map,并利用StrSubstitutor的replace方法,可以将这些值注入到模板字符串中,实现快速生成带有实际内容的字符串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

引入 import org.apache.commons.lang3.text.StrSubstitutor; 包

public class Test {
	public static void main(String[] args) {
		Map valuesMap = new HashMap();
		valuesMap.put("animal", "quick brown fox");
		valuesMap.put("target", "lazy dog");
		String templateString = "The ${animal} jumped over the ${target}.";
		StrSubstitutor sub = new StrSubstitutor(valuesMap);
		String resolvedString = sub.replace(templateString);
		System.out.println(resolvedString);
	}
}
``` protected McTaskMsgContentEntity getTaskMsgContent( String templateId, String channelType, McTaskMsgEntity taskMsg, McMsgTemplateEntity msgTemplate, McMsgAccountEntity account ) { String taskMsgId = taskMsg.getId(); // 消息场景 McBusinessInfoVO businessInfoVO = mcBusinessService.getInfoByEnCode(msgTemplate.getMessageSource()); // 业务消息关键词 String businessKey = taskMsg.getBusinessKey(); Map<String, Object> businessKeyMap = JsonUtil.stringToMap(businessKey); // 标题(带有占位符的) String title = msgTemplate.getTitle(); // 内容(带有占位符的) String content = msgTemplate.getContent(); // 摘要 String description = null; // 消息链接 String linkUrl = null; // 图片链接 String picUrl = null; // 替换参数 if (StringUtils.isNotEmpty(businessKeyMap)) { // 标题 if (StringUtils.isNotEmpty(title)) { StringSubstitutor strSubstitutor = new StringSubstitutor(businessKeyMap, "{", "}"); title = strSubstitutor.replace(title); } else { title = String.valueOf(businessKeyMap.get("title")); } // 内容 if (StringUtils.isNotEmpty(content)) { StringSubstitutor strSubstitutor = new StringSubstitutor(businessKeyMap, "{", "}"); content = strSubstitutor.replace(content); } else { content = String.valueOf(businessKeyMap.get("content")); } // 摘要 if (businessKeyMap.containsKey("desc") && StringUtils.isNotEmpty(businessKeyMap.get("desc").toString())) { description = businessKeyMap.get("desc").toString(); } else { description = StringUtils.isEmpty(content) ? null : StringUtils.substring(content, 0, 200); } // 消息链接 linkUrl = this.packLinkUrl(channelType, businessKeyMap, account, taskMsgId); // 图片链接 if (businessKeyMap.containsKey("picUrl") && ObjectUtil.isNotEmpty(businessKeyMap.get("picUrl"))) { picUrl = String.valueOf(businessKeyMap.get("picUrl")); } } // 封装业务消息内容实体 McTaskMsgContentEntity taskMsgContentEntity = new McTaskMsgContentEntity(); // 标题 if (StringUtils.isNotEmpty(title)) { // title = StringUtils.stringFilter(title); title = SensitiveWordUtil.sensitiveFilter(title, true); title = StringUtils.substring(title, 0, 50); taskMsgContentEntity.setTitle(title); } // 内容 if (StringUtils.isNotEmpty(content)) { content = SensitiveWordUtil.sensitiveFilter(content, true); taskMsgContentEntity.setContent(content); } // 摘要 if (StringUtils.isNotEmpty(description)) { description = SensitiveWordUtil.sensitiveFilter(description, true); taskMsgContentEntity.setDescription(description); } // 消息链接 taskMsgContentEntity.setLinkUrl(linkUrl); // 图片链接 if (StringUtils.isNotEmpty(picUrl)) { taskMsgContentEntity.setPicUrl(picUrl); } taskMsgContentEntity.setMsgAccountId(ObjectUtil.isEmpty(account) ? null : account.getId()); return taskMsgContentEntity; }```Refactor this method to reduce its Cognitive Complexity from 22 to the 15 allowed.
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早退的程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值