Here you can find the source of replaceToStringBuilder(String s, String begin, String end, Map
public static StringBuilder replaceToStringBuilder(String s, String begin, String end, Map<String, String> values)
//package com.java2s; //License from project: Open Source License import java.util.Map; public class Main { public static StringBuilder replaceToStringBuilder(String s, String begin, String end, Map<String, String> values) { if ((s == null) || (begin == null) || (end == null) || (values == null) || (values.size() == 0)) { return new StringBuilder(s); }/*from w ww. jav a2 s .c om*/ StringBuilder sb = new StringBuilder(values.size() * 2 + 1); int pos = 0; while (true) { int x = s.indexOf(begin, pos); int y = s.indexOf(end, x + begin.length()); if ((x == -1) || (y == -1)) { sb.append(s.substring(pos)); break; } else { sb.append(s.substring(pos, x)); String oldValue = s.substring(x + begin.length(), y); String newValue = values.get(oldValue); if (newValue == null) { newValue = oldValue; } sb.append(newValue); pos = y + end.length(); } } return sb; } }