Procedural design Implementing a String Replace Function

page 69 private static String strChangeString string, String from, String to { String before = , after = ; int index; index = string.indexOffrom; while index = 0 { before = string.substring0, index; after = string.substringindex + from.length; string = before + to + after; index = string.indexOffrom, index + to.length; } return string; }

4.6.1.2 Recursive design

To implement a string replace function with recursion, we take a modified version of the approach we used here. We build the replaced string in three pieces: • Everything up to the first occurrence of the substring were replacing. If the substring doesnt exist in the main string, then this is the entire string. • The replacement substring. If the substring were replacing doesnt exist in the main string, then this is blank. • Everything after the first occurrence of the substring. If the substring doesnt exist in the main string, then this is blank. The third portion is where we use recursion. If the substring were replacing occurs in that part of the main string, we call the substring replace function on the last of the string. The key here, as with all recursive functions, is that we have an exit case, a condition in which we dont recurse. If the substring doesnt occur in the last portion of the string, were done. Heres the design in pseudocode: replaceSubstringoriginalString, substring, replacementString { if containsoriginalString, substring firstOfString = substring-beforeoriginalString, substring else firstOfString = originalString if containsoriginalString, substring middleOfString = replacementString else middleOfString = if containsoriginalString, substring { if containssubstring-afteroriginalString, substring, substring lastOfString = replaceStringsubstring-afteroriginalString, substring, substring, replacementString else lastOfString = substring-afteroriginalString, substring } concatfirstOfString, middleOfString, lastOfString } page 70 In the recursive approach, the function calls itself whenever theres at least one more occurrence of the substring. Each time the function calls itself, the originalString parameter is a little smaller, until eventually weve processed the complete string. Heres the complete template: xsl:template name=replace-substring xsl:param name=original xsl:param name=substring xsl:param name=replacement select= xsl:variable name=first xsl:choose xsl:when test=containsoriginal, substring xsl:value-of select=substring-beforeoriginal, substring xsl:when xsl:otherwise xsl:value-of select=original xsl:otherwise xsl:choose xsl:variable xsl:variable name=middle xsl:choose xsl:when test=containsoriginal, substring xsl:value-of select=replacement xsl:when xsl:otherwise xsl:textxsl:text xsl:otherwise xsl:choose xsl:variable xsl:variable name=last xsl:choose xsl:when test=containsoriginal, substring xsl:choose xsl:when test=containssubstring-afteroriginal, substring, substring xsl:call-template name=replace-substring xsl:with-param name=original xsl:value-of select=substring-afteroriginal, substring xsl:with-param xsl:with-param name=substring xsl:value-of select=substring xsl:with-param xsl:with-param name=replacement xsl:value-of select=replacement xsl:with-param xsl:call-template xsl:when xsl:otherwise xsl:value-of select=substring-afteroriginal, substring xsl:otherwise xsl:choose xsl:when xsl:otherwise xsl:textxsl:text xsl:otherwise xsl:choose xsl:variable xsl:value-of select=concatfirst, middle, last xsl:template This style of programming takes some getting used to, but whatever you want to do can usually be done. Our example here is a good illustration of the techniques weve discussed in this chapter, including branching statements, variables, invoking templates by name, and passing parameters.