Function String Replacements Class

ReplaceCharacter

Replaces all occurrences of one character within a string with another character.

Signature (Visual Basic / VBA)

Public Function ReplaceCharacter( _
      ByVal vSearch As Variant _
    , ByVal vFromChar As Variant _
    , ByVal vToChar As Variant _
    , Optional ByVal vCompare As Variant _
    ) As Variant

Code Example

    ReplaceCharacter("sample set", "s", "S") = "Sample Set"
    ReplaceCharacter("On the town", " ", "-") = "On-the-town"
    ReplaceCharacter("On the town", " ", "") = "On the town" ' Behavior #6

Parameters

Parameter Description
vSearch The string in which characters are to be replaced. Function returns Null if vSearch is Null or cannot be fixed up to a String (Behavior #1).
vFromChar The character which is to be replaced with vToChar wherever it appears within string vSearch. Note: Special argument fix-up code: If vFromChar is an empty string, then vSearch is returned unchanged (Behavior #2). If vFromChar is a non-empty string, then only its first character is used as the search character (Behavior #3). If vFromChar is numeric (and not a numeric string), then characters with the specified ASCII value will be replaced by vToChar (Behavior #4). Function returns Null if vFromChar is not string nor numeric (Behavior #5).
vToChar The character which is to replace all occurrences of vFromChar within string vSearch. Note: Special argument fix-up code: If vToChar is an empty string, then vSearch is returned unchanged (Behavior #6). If vToChar is a non-empty string, then only its first character is used as the replacement character (Behavior #7). If vToChar is numeric (and not a numeric string), then the character with the specified ASCII value will be used as the replacement character (Behavior #8). Function returns Null if vToChar is not string nor numeric (Behavior #9).
vCompare Specifies the type of comparison used to determine if characters match (Behavior #10). vCompare defaults to Binary comparisons if it is missing or Null or cannot be fixed up to a number (Behavior #11).