Speeding Character/String Searches With the Like Operator

I have written a simple function called NeedsFiltering which I use to determine if a block of text contains one or more search characters. The function uses the Like operator to determine if one or more characters in the specified range exist in the input string. If the routine returns False, the text block can be “passed through” without the need for a more time-consuming search.

Function NeedsFiltering (szInput As String) As Integer
Dim szLikeExpr1 As String
Dim szLikeExpr2 As String
szLikeExpr1 = "*[" & Chr(0) & "-" & Chr(8) & "]*"
szLikeExpr2 = "*[" & Chr(128) & "-" & Chr(255) & "]*"
NeedsFiltering = szInput Like szLikeExpr1 Or szInput Like szLikeExpr2
End Function

The “*” in the pattern expressions means “zero or more characters.” Therefore, the Like operator will return True if one or more characters in the specified range exist anywhere in the input string. I have used this routine quite successfully in a character filtering application which filters out control and extended ANSI characters (0-8,128-255) from an input file, and places the filtered text in a temporary file. The amount of savings depends upon the number and location of characters in the input string that one wishes to filter out.


Tip Submitted By: Barth Riley