Function String Replacements Class

ReplaceStringMultiple

"Replace String Multiple Times" Performs multiple string replacements within a string.

Signature (Visual Basic / VBA)

Public Function ReplaceStringMultiple( _
      ByVal vValue As Variant _
    , ByVal vFindReplacePairs As Variant _
    , ByVal vSeparator As Variant _
    , Optional ByVal vCompare As Variant _
    ) As Variant

Code Example

    ReplaceStringMultiple("A B C", "A|Adam|B|Boy|C|Charles", "|") = "Adam Boy Charles"

Parameters

Parameter Description
vValue The string in which the replacement is to take place. Function returns Null if vValue is Null or cannot be fixed up to a String (Behavior #1).
vFindReplacePairs String containing one or more pairs of find and replace sub-strings, with each sub-string separated by the vSeparator string. For example, a vFindReplacePairs of "A|Adam|B|Boy" would tell this function to replace all occurrences of "A" in vValue with "Adam" and to replace all occurrences of "B" with "Boy". Function returns Null if vFindReplacePairs is Null or cannot be fixed up to a String (Behavior #2). Function returns vValue unchanged if vFindReplacePairs is an empty string (Behavior #3).
vSeparator The string which separates each of the substrings in vFindReplacePairs. Function uses Binary Comparison to locate the separator string vSeparator within string vFindReplacePairs (Behavior #5). Function returns Null if vSeparator is Null or cannot be fixed up to a String (Behavior #6).
vCompare Specifies the type of comparison used to determine if the find strings appear within vValue (Behavior #7). vCompare defaults to Binary comparisons if it is missing or Null or cannot be fixed up to a number (Behavior #8).

Notes & Version History

Note This function works correctly with replacement strings that contain the search/find string. For example, Chr$(10) can be replaced by Chr$(10)+Chr$(13) without the function getting into an infinite loop (Behavior #4).
v1.5 Note Behavior #6 is different than in previous versions of Entisoft Tools. In previous versions, the function would return vValue unchanged is vSeparator was Null or could not be fixed up to a String.
Note Function searches for occurrences of the find strings within vValue in the order in which the find strings appear in vFindReplacePairs. If there are ambiguous find strings such as "A", "AB", "AC", then the strings should be placed in vFindReplacePairs in ascending sort order (Behavior #9).
v1.5 BugFix The documentation for this note was incorrect in previous versions of Entisoft Tools.