Here you can find the source of replace(String s, Map
public static String replace(String s, Map<String, Object> map)
//package com.java2s; import java.util.Map; public class Main { public static String replace(String s, Map<String, Object> map) { StringBuilder ret = new StringBuilder((int) (s.length() * 1.5)); int cursor = 0; for (int start, end; (start = s.indexOf("${", cursor)) != -1 && (end = s.indexOf("}", start)) != -1;) { ret.append(s.substring(cursor, start)).append(map.get(s.substring(start + 2, end))); cursor = end + 1;//from w ww . j a v a 2s . co m } ret.append(s.substring(cursor, s.length())); return ret.toString(); } public static String replace(String s, Object... objs) { if (objs == null || objs.length == 0) return s; if (s.indexOf("{}") == -1) return s; StringBuilder ret = new StringBuilder((int) (s.length() * 1.5)); int cursor = 0; int index = 0; for (int start; (start = s.indexOf("{}", cursor)) != -1;) { ret.append(s.substring(cursor, start)); if (index < objs.length) { ret.append(objs[index]); } else { ret.append("{}"); } cursor = start + 2; index++; } ret.append(s.substring(cursor, s.length())); return ret.toString(); } }