Function String Replacements Class

ReplaceStringRecursively

Perform string replacement. Replace all possibly-overlapping occurrences of vFind in vLine with vReplace. Continue replacing string vFind in vLine with vReplace until vFind no longer appears (Behavior #9).

Signature (Visual Basic / VBA)

Public Function ReplaceStringRecursively( _
      ByVal vLine As Variant _
    , ByVal vFind As Variant _
    , Optional ByVal vReplace As Variant _
    , Optional ByVal vCompare As Variant _
    ) As Variant

Code Example

       ? ReplaceStringRecursively("This   is a    line with     a lot of     spaces.", "  ", " ")
       This is a line with a lot of spaces.
    whereas
       ? ReplaceString("This   is a    line with     a lot of     spaces.", "  ", " ")
       This  is a  line with   a lot of   spaces.
    and
       ReplaceStringRecursively("This is a test.", "s", "sjs") = "" ' Behavior #8

Parameters

Parameter Description
vLine The string in which all occurrences of vFind are to be replaced with vReplace. Function returns Null if vLine is Null or cannot be fixed up to a String (Behavior #1).
vFind The string which is to be replaced with vReplace wherever it appears within string vLine. Function returns Null if vFind is Null or cannot be fixed up to a String (Behavior #2). Function returns vLine unchanged if vFind is an empty string (Behavior #3).
vReplace The string which is to replace all occurrences of vFind within string vLine. vReplace defaults to an empty string if it is missing or Null or cannot be fixed up to a String (Behavior #4). vReplace can be an empty string in which case the first occurrence of vFind is removed from the string vLine (Behavior #5).
vCompare Specifies the type of comparison used to determine if strings match (Behavior #6). vCompare defaults to Binary comparisons if it is missing or Null or cannot be fixed up to a number (Behavior #7).

Notes & Version History

Note Function returns an empty string if vFind is a sub-string of vReplace to avoid having the it get stuck in an endless loop (Behavior #8).