Published: Thursday, December 07, 2000
Determining if a String has Certain Substrings
There are numerous times when a developer would like to be able to quickly determine
if a string contains a particular substring. Fortunately, there is the InStr
function in VBScript that can be used to quickly determine whether or not a certain substring
is within another string. The InStr function, whose technical documentation
can be found here,
has the following form:
InStr(start, SearchString, SubStringToFind[, compare])
|
The InStr function returns the starting position of SubStringToFind
in SearchString if it is found, else InStr returns a value
of zero (0). Therefore, a quick way to check if a string contains a particular
substring is to use the following line of code:
Dim strSearchString, strSearchFor
strSearchString = "Now is the time for all good men to..."
strSearchFor = "men"
If InStr(1, strSearchString, strSearchFor) > 0 then
'We found strSearchFor in strSearchString
Else
'We didn't find strSearchFor in strSearchString
End If
|
Fairly simple and straightforward... chances are you've used a similar chunk of code
an uncountable number of times yourself in your own ASP pages. However, imagine that we
have some string that we want to ensure does not have a large number of substrings? Perhaps
we are letting a user enter a value into some textarea form field and we want to make sure
that they do not have substrings like <html>, </html>,
<body>, and </body>.
Well, we could simply have multiple InStr lines, but that would get annoying
fast. Why not create a couple custom functions that will handle this for us in one or
two lines of code? Before I delve into the code, let me explain what I am going to attempt
to do here:
- First, I will create a Dictionary object that will contain, as a key, each
substring that we do not want to appear in some string. (For more information on the
Dictionary object (
Scripting.Dictionary), check out
Using the Dictionary Object!) Each value of each
Dictionary entry will initially be set to False.
- When I want to determine if a string contains any offending substrings, I will loop
through each element of my Dictionary object using
InStr to check if the
current Dictionary element's key (the offending substring) is found in the string.
If it is, I'll adjust that entry's value from False to True.
To assist with this, I've created a number of helper functions. The first one is used
to add an offending word to the Dictionary object of substrings that are not allowed:
Sub AddViolation(objDict, strWord)
'Adds a violation (a substring that is not allowed)
objDict.Add strWord, False
End Sub
|
Fairly straightforward, no? The function AddViolation expects two parameters:
the Dictionary object of offending substrings and an illegal substring. The function simply adds
a new element to the Dictionary object with the key as the offending word and a value of False.
The next helper function, CheckStringForViolations, expects two parameters:
the string to check for any offending substrings, and the Dictionary object of illegal
substrings. The function returns
Function CheckStringForViolations(strString, objDict)
'Determines if the string strString has any violations
Dim bolViolations
bolViolations = False
Dim strKey
For Each strKey in objDict
If InStr(1, strString, strKey, vbTextCompare) > 0 then
bolViolations = True
objDict(strKey) = True
End If
Next
CheckStringForViolations = bolViolations
End Function
|
The function starts off by assigning False to bolViolations; next, a For
Each ... Next loop iterates through the Dictionary object. At each iteration, the
current Dictionary object key is checked to see if it is a substring of the passed-in string
strString. If it is, bolViolations is set to True and the current
Dictionary element's value is set to True. The function concludes by returning the
value of bolViolations.
A third and final helper function, ListViolations, returns a string that contains
a list of those offending substrings found in the string:
Function ListViolations(objDict)
'Returns a string of the violations found
Dim strKey, strViolations
For Each strKey in objDict
If objDict(strKey) then
strViolations = strViolations & strKey & ","
End If
Next
ListViolations = Left(strViolations, Len(strViolations) - 1)
End Function
|
Now that we've examined these helper functions, it's time to look at them in use! We'll
look at this, plus at some major enhancements, in Part 2!
Read Part 2