scripting@wanadoo.nl




ReplaceStr()

http://www.scriptlogic.com/kixtart/FunctionLibrary_FunctionList.aspx


actual FunctionLibrary list on 'Scriptlogic' site printer-friendly mirror of UDF topic on 'scripting@wanadoo.nl' site close
4 September 2002 ReplaceStr()
AuthorLee Wilmott
Action Given a string, this function replaces either the first occurrence or all occurrences of second supplied string.
SyntaxReplaceStr ("text", "find", "replace" [,"replaceall"]) 
Parameters
  • text (Required / String) The string that is to be searched.
  • find (Required / String) The string that should be replaced.
  • replace (Required / String) The string that should replace the specified "find" text.
  • replaceall (Optional / Numeric) Specifies whether the first or all occurences of the given string should be replaced (-1 = all occurences are replaced, otherwise = first occurence replaced). 
  • RemarksI know that similar functions exist in the Function Library (Replace and StringReplace). However, they didn't seem to be able to perform the functions that I required.
    I have intentionally made the "replaceall" parameter require a "-1" value to replace all occurences. Perhaps this function would then be a little more useful as it could be modified to accept a positive number which could indicate how many occurences to replace.

    NOTE: If the text to be searched is an empty string ("") and the string that should be replaced is also an empty string ("") then this function always returns an empty string. Although I don't think this is how it should work, this is how the Replace() function in VisualBASIC operates. 
    ReturnsA string with substrings replaced with a given string. 
    DependenciesNone. 
    Examples
    ReplaceStr("xyx", "X", ".", -1)          ; .y.
    ReplaceStr("xyx", "x", ".")              ; .yx
    ReplaceStr("Wilmott", "t", "T", -1)      ; WilmoTT
    ReplaceStr("Wilmott", "t", "T")          ; WilmoTt
    
     
    Source
    FUNCTION ReplaceStr ($textstring, $findtext, $replacetext, OPTIONAL $replaceall)
      ; Given a string ($TextString), $FindText is located and replaced with $ReplaceText.
      ; If $ReplaceAll is -1 then all occurrences are replaced.
    
      DIM $textlocation
      IF ($textstring = "")
        $replacestr = ""
      ELSE
        IF ($findtext = "")
          $replacestr = $textstring
        ELSE
          IF ($replaceall = -1)
            $replacestr = Join(Split($textstring, $findtext), $replacetext)
          ELSE
            $textlocation = InStr($textstring, $findtext)
            $replacestr = Left($textstring, $textlocation-1)+$replacetext+Right($textstring, Len($textstring)-$textlocation-Len($findtext)+1)
          ENDIF
        ENDIF
      ENDIF
    ENDFUNCTION ; - ReplaceStr -
     
      original source of UDF topic. show actual FunctionLibrary list on Scriptlogic site close top
              printer-friendly mirror of UDF topic on scripting@wanadoo.nl site  




    Copyright © 2003 www.scriptlogic.com & scripting@wanadoo.nl - last updated on 20 May 2003


    Site Meter