(Mike Mitchell)
6/7/2012 7:19:00 PM
Here's a function to replace certain characters in a string with a
space:
Function ReplaceSpecialCharacters2(ByVal s As String) As String
Dim j As Long
Dim c As String
Dim ln As Long
Dim p As String
ln = Len(s)
For j = 1 To ln
c = Mid$(s, j, 1)
If InStr(StopChars, c) Then
p = p & " " ' Append a space to replace any stop character
Else
p = p & c ' else append the character
End If
Next
ReplaceSpecialCharacters2 = p
End Function
For the Stop characters, see below.
And here is the original sub:
Sub ReplaceSpecialCharacters(s As String)
' Remove/change certain characters
s = Replace(s, "*", " ")
s = Replace(s, "<", " ")
s = Replace(s, ">", " ")
s = Replace(s, "&", " ")
s = Replace(s, "{", " ")
s = Replace(s, "}", " ")
s = Replace(s, "_", " ")
s = Replace(s, "?", " ")
s = Replace(s, ".", " ")
s = Replace(s, "!", " ")
s = Replace(s, ":", " ")
s = Replace(s, ";", " ")
s = Replace(s, ",", " ")
s = Replace(s, "(", " ")
s = Replace(s, ")", " ")
s = Replace(s, "[", " ")
s = Replace(s, "]", " ")
s = Replace(s, "-", " ")
s = Replace(s, Chr$(34), " ")
s = Replace(s, "'", " ")
End Sub
I've done a few speed comparisons and the function is always slower by
between 7 and 16 seconds.
The speed test consists of reading in the Subject line of 640,000
Usenet messages and passing each subject text through either the
function or the sub, depending on a temporary checkbox setting on the
form.
The reason I rewrote the Sub as a Function is because I assumed that
using Replace umpteen times would definitely be slower, but it isn't,
it's faster.
Why?
MM