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?
Who is Participating?
kalihtoConnect With a Mentor Commented:
Try this function

'   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
End Function

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.
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.
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.
Private Sub Command1_Click()
'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
End Function
All Courses

From novice to tech pro — start learning today.