Android String Replace replace(String line, String oldString, String newString)

Here you can find the source of replace(String line, String oldString, String newString)

Description

replace

Declaration

public static final String replace(String line, String oldString,
        String newString) 

Method Source Code

//package com.java2s;

public class Main {

    public static final String replace(String line, String oldString,
            String newString) {//from   w w  w .  jav a2  s  . com
        if (line == null) {
            return null;
        }
        int i = 0;
        if ((i = line.indexOf(oldString, i)) >= 0) {
            char[] line2 = line.toCharArray();
            char[] newString2 = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j = i;
            while ((i = line.indexOf(oldString, i)) > 0) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
                j = i;
            }
            buf.append(line2, j, line2.length - j);
            return buf.toString();
        }
        return line;
    }
}

Related

  1. replace(String from, String to, String source)
  2. replace(String originalString, String searchString, String replaceString)
  3. replace(String str, Map replacementMap)
  4. replace(final String text, final String fromText, final String toText)
  5. replaceAll(String input, String searchStr, String replaceWithStr)