Java string multiple replacement

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

Java string multiple replacement
Java string
String replacement
[Java] String padding
Java string processing
Split string (Java)
[Java] String comparison and && and ||
[Note] Java: String search
[Note] Java: String survey
About Java String class
Java inflexible String class substring
Reflection on Java string manipulation
[Java] About String and StringBuilder
[Java] Multiple OR condition judgment
[Java] Combine multiple Lists (Collections)
[Java] Speed comparison of string concatenation
Operation to connect multiple Streams @Java
Various methods of Java String class
Arbitrary string creation code by Java
Java
String
Studying Java 8 (String Joiner and join)
Java
[Java] Correct comparison of String type
[Java & Kotlin] Create multiple selectable RecyclerView
Catch multiple exceptions together in java
[Java] Divide a character string by a specified character
When seeking multiple in a Java array
Java date data type conversion (Date, Calendar, String)
Full-width → half-width conversion with Java String (full-width kana → half-width kana)
Regarding String type equivalence comparison in Java
[Java] Data type / string class cheat sheet
Sort by multiple conditions using Java Stream
Notes on operators using Java ~ String type ~
Java String Byte string truncation Supports garbled characters
[Java] Convert Object type null to String type
How to output Java string to console screen
[Java] Character judgment / character string formatting (AOJ11 --character count)
All same hash code string in Java
Split a string with ". (Dot)" in Java
[Java] Comparison of String type character strings
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
How to switch between multiple Java versions
Cheking Prime number --Java string program examples
Type conversion from java BigDecimal type to String type
Java installation (Mac, Homebrew, multiple version control)