Standard library A little complicated but may be fast
String str = "$aTEST$bTESTTEST$cTESTTEST$aTEST$bTESTTEST$cTESTTEST";
//Before replacement,After replacement
Map<String, String> map = new HashMap<>();
map.put("$a", "AaA");
map.put("$b", "BbB");
//・
//・
//Replacement process(java.util.regex.Matcher)
Matcher matcher = Pattern.compile(map.keySet().stream().map(Pattern::quote).collect(Collectors.joining("|"))).matcher(str);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, map.get(matcher.group()));
}
matcher.appendTail(sb);
sb.toString();
appache commons library Concise but slower than I expected. .. ..
String str = "$aTEST$bTESTTEST$cTESTTEST$aTEST$bTESTTEST$cTESTTEST";
//Before replacement,After replacement
Map<String, String> map = new HashMap<>();
map.put("$a", "AaA");
map.put("$b", "BbB");
//・
//・
//Replacement process(org.apache.commons.lang3.StringUtils)
StringUtils.replaceEach(str, map.keySet().toArray(new String[map.size()]), map.values().toArray(new String[map.size()]));
Recommended Posts