Here you can find the source of replaceAll(String input, String searchStr, String replaceWithStr)
public static String replaceAll(String input, String searchStr, String replaceWithStr)
//package com.java2s; /**// ww w.j av a 2 s . co m * Copyright (c) {2003,2011} {openmobster@gmail.com} {individual contributors as indicated by the @authors tag}. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ public class Main { public static String replaceAll(String input, String searchStr, String replaceWithStr) { StringBuffer buffer = new StringBuffer(); int startIndex = 0; int oldIndex = 0; if (input.indexOf(searchStr) == -1) { return input; } while ((startIndex = input.indexOf(searchStr, oldIndex)) != -1) { buffer.append(input.substring(oldIndex, startIndex)); buffer.append(replaceWithStr); startIndex += searchStr.length(); oldIndex = startIndex; if (oldIndex >= input.length()) { break; } } if (oldIndex < input.length()) { buffer.append(input.substring(oldIndex)); } return buffer.toString(); } }