Function CharTran(ByVal sTargetStr As String, ByVal sTargetChars As String, _
Optional ByVal sReplacementChars As String = "", Optional ByVal iCompMethod As Integer = 0) As String
'====================================================================================================================================
' Purpose : Replaces the targeted existing character(s) in the source string with corresponding ones in the given replacement
' string.
' Description : Checks if the passed parameters' data types match those of the Function's arguments; if not – throws error message
' and returns empty string, except the case when Replacement Chars is blank – then Target String returned unchanged.
' Runs built-in String.Replace() system function consequently for each char in the Target Chars string to replace
' those with corresponding Replacement Char.
' Parameters : Target string that needs to be updated;
' String of the characters to be replaced;
' String of the replacement characters;
' Comparison method.
' Returns : Updated string as String.
' Side effects : None.
' Notes : 1. Generic, complies with .NET Framework ver. 1.1 and higher.
' 2. If the Replacement Chars string is empty (default) – all the characters of the Target Chars in the Target
' string will be deleted, empty string returned.
' 3. If Comparison method is 0 (default) – the comparison will be case-sensitive, if 1 – case-insensitive.
' 4. All characters in the Target String matching those in the Replacement String will be replaced. Example:
' sTargetStr = "Aha", sTargetChars = "a", sReplacementChars = "u", iCompMethod = 1 (case-insensitive) => "uhu"
' - '' - - '' - - '' - iCompMethod = 0 (case-sensitive) => "Ahu"
' 5. If a Char member of Target Chars set not found in the Target String - it's ignored.
' 6. If a Char member of Target Chars set found in the Target String but has no Replacement Char of the corresponding
' sequential number (e.g. #5 in Target Chars, but there are only 4 Replacement Chars) - it's ignored.
' Author : Ilya I. Rabyy
' Revisions : 2019-12-19 by Ilya – started 1st draft.
'====================================================================================================================================
' Memvars' declaration
Dim sMsg As String, lGoOn As Boolean, sRetStr As String
lGoOn = True ' Initially
sRetStr = "" ' Initially
' Non-optional parameters' verification
If VarType(sTargetStr) <> vbString Then
sMsg = "Invalid parameter passed: sTargetStr must be of type String!"
lGoOn = False
ElseIf String.IsNullOrEmpty(sTargetStr) Then
' No need to scream, just return that blank string
lGoOn = False
ElseIf VarType(sTargetChars) <> vbString Then
sMsg = "Invalid parameter passed: sTargetChars must be of type String!"
lGoOn = False
ElseIf String.IsNullOrEmpty(sTargetChars) Then
' Same as with Target String: no screaming, just return the original string
sRetStr = sTargetStr
lGoOn = False
End If
If lGoOn = False Then
Return sRetStr
End If
' Special case: calling proc wants to change all the chars in given string to the same single char,
' e.g. "you" to "U", and the Replacement string is just "U"
If Len(sReplacementChars) = 1 Then sReplacementChars = StrDup(Len(sTargetChars), sReplacementChars)
' Done with parameters' verification part.
' More memvars' declaration
Dim iSrcPos As Integer, cChar2Change As Char, iStrLen As Integer
sRetStr = sTargetStr ' Initially
iStrLen = Len(sTargetChars)
' Cycle thru the targeted chars and replace them with their counterparts from Replacement string
For iSrcPos = 1 To iStrLen
cChar2Change = Mid(sTargetChars, iSrcPos, 1)
If AscW(cChar2Change) = 173 Or AscW(cChar2Change) = 178 Then Continue For
' Gotta skip char 173 coz it nullifies the whole string (why?), and shar 178 cuts off the last char in the sRetStr (why?)
'Debug, 2019-12-23 - losing the last character in the given string - ?
If Asc(cChar2Change) = 128 Then Stop ' The only Q. mark in the last sequence "?_V01"
If String.IsNullOrEmpty(sReplacementChars) Then ' The calling proc just wants to delete this Char from the given string - OK!
sRetStr = Replace(sRetStr, cChar2Change, "", 1, -1, iCompMethod)
Else
sRetStr = Replace(sRetStr, cChar2Change, IIf(iSrcPos <= Len(sReplacementChars), Mid(sReplacementChars, iSrcPos, 1), ""), 1, -1, iCompMethod)
End If
Next iSrcPos
Return sRetStr
End Function