Here you can find the source of replaceWords(String src, String targetValue, String newValue)
public static String replaceWords(String src, String targetValue, String newValue)
//package com.java2s; public class Main { public static String replaceWords(String src, String targetValue, String newValue) {// w w w . j a v a2 s. com if (targetValue.equals("")) { throw new IllegalArgumentException( "Old pattern must have content."); } StringBuffer result = new StringBuffer(); int startIdx = 0; int idxOld = src.indexOf(targetValue, startIdx); if (idxOld >= 0) { result.append(src.substring(startIdx, idxOld)); result.append(newValue); startIdx = idxOld + targetValue.length(); } result.append(src.substring(startIdx)); return result.toString(); } }