Here you can find the source of substitute(String in, String find, String newString)
substitute returns a string in which 'find' is substituted by 'newString'
Parameter | Description |
---|---|
in | String to edit |
find | string to match |
newString | string to substitude for find |
public static String substitute(String in, String find, String newString)
//package com.java2s; public class Main { /**//from w w w .j a va 2 s .co m * <p> * substitute returns a string in which 'find' is substituted by 'newString' * * @param in String to edit * @param find string to match * @param newString string to substitude for find * @return The edited string */ public static String substitute(String in, String find, String newString) { // when either of the strings are null, return the original string if (in == null || find == null || newString == null) return in; char[] working = in.toCharArray(); StringBuffer stringBuffer = new StringBuffer(); // when the find string could not be found, return the original string int startindex = in.indexOf(find); if (startindex < 0) return in; int currindex = 0; while (startindex > -1) { for (int i = currindex; i < startindex; i++) { stringBuffer.append(working[i]); } // for currindex = startindex; stringBuffer.append(newString); currindex += find.length(); startindex = in.indexOf(find, currindex); } // while for (int i = currindex; i < working.length; i++) { stringBuffer.append(working[i]); } // for return stringBuffer.toString(); } }