Here you can find the source of replaceFromMap(final String string, final Map
public static String replaceFromMap(final String string, final Map<String, String> replacements)
//package com.java2s; //License from project: Open Source License import java.util.*; public class Main { public static String replaceFromMap(final String string, final Map<String, String> replacements) { StringBuilder sb = new StringBuilder(string); for (Map.Entry<String, String> entry : replacements.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); final int start = sb.indexOf(key, 0); replaceString(sb, key, value, start); }/* w w w. j av a 2 s . com*/ return sb.toString(); } private static void replaceString(final StringBuilder sb, final String key, final String value, int start) { while (start > -1) { final int end = start + key.length(); final int nextSearchStart = start + value.length(); sb.replace(start, end, value); start = sb.indexOf(key, nextSearchStart); } } public static String replace(final String string, final String target, final String replacement) { final StringBuilder sb = new StringBuilder(string); final int start = sb.indexOf(target, 0); replaceString(sb, target, replacement, start); return sb.toString(); } }