Android String Replace replaceAll(String input, String searchStr, String replaceWithStr)

Here you can find the source of replaceAll(String input, String searchStr, String replaceWithStr)

Description

replace All

License

Open Source License

Declaration

public static String replaceAll(String input, String searchStr,
            String replaceWithStr) 

Method Source Code

//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();
    }
}

Related

  1. replace(String from, String to, String source)
  2. replace(String line, String oldString, String newString)
  3. replace(String originalString, String searchString, String replaceString)
  4. replace(String str, Map replacementMap)
  5. replace(final String text, final String fromText, final String toText)
  6. replaceAllKanaWith(String text, String replacement)
  7. replaceAllKanjiWith(String text, String replacement)
  8. replaceKeyWords(StringBuilder sb, String[] keywords, String replaceStr)
  9. replaceR(String str)