Best way to check a string for a list of words

I have a list of "banned words," and I have a string.  I want to check the string to see if it has any of the banned words in it.

What is the best way to go about this?
bomaxAsked:
Who is Participating?
 
kalihtoConnect With a Mentor Commented:
Try this function
-----------------

   
'Usage:
'   If HasBannedWord("this is a b3", "b1", "b2", "b3") Then
'     Msgbox "good statement"
'   else
'     Msgbox "Dont use a banned word"
'   end if
Public Function HasBannedWord(ByVal strString As String, ParamArray BannedWords()) As Boolean
    HasBannedWord = False
Dim i&
    For i = 0 To UBound(BannedWords)
        If InStr(strString, BannedWords(i)) > 0 Then
            HasBannedWord = True
            Exit Function
        End If
    Next
End Function

0
 
SethiCommented:
Use Instr function in VB. If the function returns a value more than equal to 1 then it means that the word is there in the string.
instr(1,strString,strProhibitedWord)
0
 
bomaxAuthor Commented:
Right, but I figured that the InStr fuction means if strString contains ALL OFF strProhibitedWord then return true.  I.e. the string would need to contain all the banned words, and it would not return true if it contained just ONE of those words in the list.
0
 
Ryan ChongCommented:
You can do a multiple checking using Intr function to check it , if all of the items in list to compare matched then return true else return false.
0
 
DocMCommented:
Private Sub Command1_Click()
List1.Clear
'list of "banned words"
List1.AddItem "Word1"
List1.AddItem "Word2"
List1.AddItem "Word3"

strstring = "This is an example string with Word1"

answer = Check_String_For_Banned_Words(strstring)
 
End Sub

Function Check_String_For_Banned_Words(strstring) As String
 For i = 0 To List1.ListCount - 1
  If InStr(strstring, List1.List(i)) > 0 Then
   Check_String_For_Banned_Words = List1.List(i)
  End If
 Next
End Function
0
All Courses

From novice to tech pro — start learning today.