一、代码示例:
三种替换
- List
knowledgeList = Optional.ofNullable(list).orElse(new ArrayList<>()).stream().map(item -> { - if (StringUtils.contains(item.getRelevanceKnowIds(), knowledgeRobotId)) {
- String relevanceKnowIds = item.getRelevanceKnowIds();
- relevanceKnowIds = StringUtils.replace(relevanceKnowIds, knowledgeRobotId + ",", "");
- relevanceKnowIds = StringUtils.replace(relevanceKnowIds, "," + knowledgeRobotId, "");
- relevanceKnowIds = StringUtils.replace(relevanceKnowIds, knowledgeRobotId, "");
- item.setRelevanceKnowIds(relevanceKnowIds);
- return item;
- }
- return item;
- }).collect(Collectors.toList());
二、原理
- StringUtils.replace(null, *, *) = null
- StringUtils.replace("", *, *) = ""
- StringUtils.replace("any", null, *) = "any"
- StringUtils.replace("any", *, null) = "any"
- StringUtils.replace("any", "", *) = "any"
- StringUtils.replace("aba", "a", null) = "aba"
- StringUtils.replace("aba", "a", "") = "b"
- StringUtils.replace("aba", "a", "z") = "zbz"