package moreservlets.filters;

/** Small utility to assist with response wrappers that
 *  return strings.
 *  <P>
 *  Taken from More Servlets and JavaServer Pages
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.moreservlets.com/.
 *  &copy; 2002 Marty Hall; may be freely used or adapted.
 */

public class FilterUtils {

  /** Change all occurrences of orig in mainString to
   *  replacement.
   */

  public static String replace(String mainString,
                               String orig,
                               String replacement) {
    String result = "";
    int oldIndex = 0;
    int index = 0;
    int origLength = orig.length();
    while((index = mainString.indexOf(orig, oldIndex))
          != -1) {
      result = result +
               mainString.substring(oldIndex, index) +
               replacement;
      oldIndex = index + origLength;
    }
    result = result + mainString.substring(oldIndex);
    return(result);
  }
}
