?
Solved

Best way to check a string for a list of words

Posted on 2003-02-28
5
Medium Priority
?
209 Views
Last Modified: 2010-05-03
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?
0
Comment
Question by:bomax
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 18

Expert Comment

by:Sethi
ID: 8046697
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
 

Author Comment

by:bomax
ID: 8046706
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
 
LVL 53

Expert Comment

by:Ryan Chong
ID: 8046809
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
 
LVL 3

Expert Comment

by:DocM
ID: 8047208
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
 

Accepted Solution

by:
kalihto earned 80 total points
ID: 8047275
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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question