by Matt Hogg (2 Submissions)
Category: String Manipulation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Mon 8th December 2003
Date Added: Mon 8th February 2021
Rating:
(1 Votes)
Keep function. A bit of a twist on the Replace function where text is submitted and a mask is also supplied. Only characters that are both in
'* Keep - Opposite of replace. Replaces all chars bar the ones specified in the mask *
'* ========================================================================================== *
'* In: *
'* vntText - The text to perform the function on *
'* strMask - The characters to keep from vntText *
'* Out: *
'* Keep - The text characters that are referenced in the mask *
'* ========================================================================================== *
'* Usage: *
'* *
'* Example: strNumber = Keep(strMixedText, "1234567890") *
'* *
'* For the above example, if strMixedText contained "AB34-23D" then Keep would return "3423" *
'* ========================================================================================== *
'* Written: Matt Hogg *
'* Date: 10 March 2003 *
Public Function Keep(ByVal vntText As Variant, ByVal strMask As String) As Variant
Dim strReplace As String 'Characters to remove
'If the text is Null then the output must be Null
Keep = Null
If IsNull(vntText) Then Exit Function
'Make a copy of what to keep
strReplace = CStr(vntText)
'Take away all mask characters and you are left with all characters to remove
Do While strMask <> ""
'Use the replace method as it does multiple chars. We only need to note distinct chars.
strReplace = Replace(strReplace, Left$(strMask, 1), "")
'Repetitions of characters in the mask are redundant!
strMask = Replace(strMask, Left$(strMask, 1), "")
Loop
'strReplace contains all non-mask characters. Remove them from the original text.
Do While strReplace <> ""
'Remove non-mask characters from original text
vntText = Replace(vntText, Left$(strReplace, 1), "")
'Repetitions of characters are redundant! Do the next character.
strReplace = Replace(strReplace, Left$(strReplace, 1), "")
Loop
'Set the return value to be only those characters from the original text that are also
'in the Mask
Keep = vntText
End Function
No comments have been posted about Keep function. A bit of a twist on the Replace function where text is submitted and a mask is also . Why not be the first to post a comment about Keep function. A bit of a twist on the Replace function where text is submitted and a mask is also .