-
Notifications
You must be signed in to change notification settings - Fork 0
/
redact_non_alpha_numeric.bas
114 lines (103 loc) · 4.17 KB
/
redact_non_alpha_numeric.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
Attribute VB_Name = "redact_non_alpha_numeric"
Option Explicit
Public Function funcRedactNonAlphaNumeric(ByVal strTarget As String) As String
funcRedactNonAlphaNumeric = strTarget
Dim a$, b$, c$, i As Integer
'The dollar sign forces the variable to return a string type rather than an undeclared variant.
'This is faster and this procedure needs to be as fast as it can be.
a$ = strTarget
For i = 1 To Len(a$)
b$ = Mid(a$, i, 1)
If b$ Like "[A-Za-z0-9 ]" Then
c$ = c$ & b$
Else
c$ = c$ & ""
End If
Next i
If c$ <> strTarget Then
funcRedactNonAlphaNumeric = Trim(c$)
End If
End Function
Private Sub TestRedactNonAlphaNumeric()
'Place your cursor in this procedure and click the play button.
'Make sure you have the Immediate Window showing (Ctrl + G)
Dim strTest As String
'strTest = "This is a test."
strTest = "It's a NP-3607"
Debug.Print "Remove nonalphanumerics from """ & strTest & """ => " & funcRedactNonAlphaNumeric(strTest)
strTest = "Did you see that the period (.) got removed?"
Debug.Print "Remove nonalphanumerics from """ & strTest & """ => " & funcRedactNonAlphaNumeric(strTest)
strTest = "Jenny's Phone# is 867-5309!"
Debug.Print "Remove nonalphanumerics from """ & strTest & """ => " & funcRedactNonAlphaNumeric(strTest)
End Sub
'===========================================
'= =
'= OTHER VERSIONS OF THE ABOVE PROCEDURE =
'= =
'===========================================
' ALTERNATE PROCEDURE NO. 1
' REMOVES NON ALPHANUMERIC CHARACTERS INCLUDING SPACES
' THIS VERSION REDUCES THE LENGTH OF THE ORIGINAL INPUT
' Example: "It's a NP-3607." becomes "ItsaNP3607"
'Public Function funcRedactNonAlphaNumeric(ByVal strTarget As String) As String
' funcRedactNonAlphaNumeric = strTarget
' Dim a$, b$, c$, i As Integer
' 'The dollar sign forces the variable to return a string type rather than an undeclared variant.
' 'This is faster and this procedure needs to be as fast as it can be.
' a$ = strTarget
' For i = 1 To Len(a$)
' b$ = Mid(a$, i, 1)
' If b$ Like "[A-Za-z0-9]" Then
' c$ = c$ & b$
' Else
' c$ = c$ & ""
' End If
' Next i
' If c$ <> strTarget Then
' funcRedactNonAlphaNumeric = Trim(c$)
' End If
'End Function
' ALTERNATE PROCEDURE NO. 2
' REMOVES NON ALPHANUMERIC CHARACTERS BUT REPLACES THEM WITH A BLANK SPACE
' THIS VERSION MAINTAINS THE ORIGINAL LENGTH OF THE USER INPUT
' Example: "It's a NP 3607." becomes "It s a NP 3607"
'Public Function funcRedactNonAlphaNumeric(ByVal strTarget As String) As String
' funcRedactNonAlphaNumeric = strTarget
' Dim a$, b$, c$, i As Integer
' 'The dollar sign forces the variable to return a string type rather than an undeclared variant.
' 'This is faster and this procedure needs to be as fast as it can be.
' a$ = strTarget
' For i = 1 To Len(a$)
' b$ = Mid(a$, i, 1)
' If b$ Like "[A-Za-z0-9]" Then
' c$ = c$ & b$
' Else
' c$ = c$ & " "
' End If
' Next i
' If c$ <> strTarget Then
' funcRedactNonAlphaNumeric = Trim(c$)
' End If
'End Function
' ORIGINAL PROCEDURE
' REMOVES NON ALPHANUMERIC CHARACTERS BUT DOES NOT REMOVE USER TYPED SPACES
' THIS VERSION REDUCES THE LENGTH OF THE USER INPUT
' Example: "It's a NP 3607." becomes "Its a NP3607"
'Public Function funcRedactNonAlphaNumeric(ByVal strTarget As String) As String
' funcRedactNonAlphaNumeric = strTarget
' Dim a$, b$, c$, i As Integer
' 'The dollar sign forces the variable to return a string type rather than an undeclared variant.
' 'This is faster and this procedure needs to be as fast as it can be.
' a$ = strTarget
' For i = 1 To Len(a$)
' b$ = Mid(a$, i, 1)
' If b$ Like "[A-Za-z0-9 ]" Then
' c$ = c$ & b$
' Else
' c$ = c$ & ""
' End If
' Next i
' If c$ <> strTarget Then
' funcRedactNonAlphaNumeric = Trim(c$)
' End If
'End Function