Java tutorial
//package com.java2s; public class Main { private static StringBuilder replaceString(StringBuilder text, String search, String replace) { int fromIndex = 0; int start = text.indexOf(search, fromIndex); if (start == -1) { return text; } if (replace.length() > 0) { int end = 0; int endAdjust = (search.length() - replace.length()); do { end = (start + replace.length()) + endAdjust; text.replace(start, end, replace); fromIndex = end; } while ((start = text.indexOf(search, fromIndex)) != -1); } else { do { text.delete(start, search.length()); fromIndex = start + replace.length(); } while ((start = text.indexOf(search, fromIndex)) != -1); } return text; } }