Check if string contains any part of string collection

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 10305 Views - Last Post: 24 September 2012 - 09:11 PM Rate Topic: -----

#1 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Check if string contains any part of string collection

Posted 22 September 2012 - 06:46 PM

My code:


Dim title As String = "test"
        For i = 0 To My.Settings.SpamFilter.Count - 1
            If title.Contains(My.Settings.SpamFilter.Item(i).ToString) Then
                MsgBox("Does contain : " & title)

            Else

                MsgBox("Does not contain : " & title)
            End If
        Next



In the spam filter collection, one of the items is test, and the result is "does not contain : test"

Is This A Good Question/Topic? 0
  • +

Replies To: Check if string contains any part of string collection

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14039
  • View blog
  • Posts: 56,188
  • Joined: 12-June 08

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 08:41 PM

What is "title"?
Was This Post Helpful? 0
  • +
  • -

#3 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 09:12 PM

A string.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14039
  • View blog
  • Posts: 56,188
  • Joined: 12-June 08

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 09:35 PM

.. and then what is "SpamFilter" in the 'my.settings.SpamFilter'?
Was This Post Helpful? 0
  • +
  • -

#5 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 10:00 PM

View Postmodi123_1, on 22 September 2012 - 09:35 PM, said:

.. and then what is "SpamFilter" in the 'my.settings.SpamFilter'?


A specialized string collection with some items in it.
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14039
  • View blog
  • Posts: 56,188
  • Joined: 12-June 08

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 10:03 PM

Well I would slap a break point there and make sure you are getting the right object. Since "specialized string collection" isn't really explained that would be the first place to look.
Was This Post Helpful? 0
  • +
  • -

#7 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 10:12 PM

View Postmodi123_1, on 22 September 2012 - 10:03 PM, said:

Well I would slap a break point there and make sure you are getting the right object. Since "specialized string collection" isn't really explained that would be the first place to look.


The items are in there correctly, I checked. I am having trouble though with checking if the string title contains any part of the SpamFilter's items.
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14039
  • View blog
  • Posts: 56,188
  • Joined: 12-June 08

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 10:15 PM

I am doubting that... Seriously.. put a break point in and see what object you are comparing it with. Things like a collection of datarows doesn't show a single cell unless you specify it. A specialized collection of strings sounds like there's odd complexity that needs to be checked. Maybe it uses a '.value' to get the string component or some other property? It's hard to tell with the little information.

Second.. this 'specialized collection' should, in theory, have a .contains of it's own.. you know.. taking care of it so you just feed it a string and it checks all the possible items in its list.
Was This Post Helpful? 0
  • +
  • -

#9 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Re: Check if string contains any part of string collection

Posted 22 September 2012 - 10:28 PM

View Postmodi123_1, on 22 September 2012 - 10:15 PM, said:

I am doubting that... Seriously.. put a break point in and see what object you are comparing it with. Things like a collection of datarows doesn't show a single cell unless you specify it. A specialized collection of strings sounds like there's odd complexity that needs to be checked. Maybe it uses a '.value' to get the string component or some other property? It's hard to tell with the little information.

Second.. this 'specialized collection' should, in theory, have a .contains of it's own.. you know.. taking care of it so you just feed it a string and it checks all the possible items in its list.


It does have its own .contains
Was This Post Helpful? 0
  • +
  • -

#10 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14039
  • View blog
  • Posts: 56,188
  • Joined: 12-June 08

Re: Check if string contains any part of string collection

Posted 23 September 2012 - 01:28 AM

... then why aren't you using it?

FYI it's becoming an exhausting time trying to guess what this magical "specialized string collection" is or does.. again, break point, view the object you are trying to compare, and go from there. I am fairly certain this is the issue.
Was This Post Helpful? 0
  • +
  • -

#11 lucky3   User is offline

  • Friend lucky3 As IHelpable
  • member icon

Reputation: 233
  • View blog
  • Posts: 770
  • Joined: 19-October 11

Re: Check if string contains any part of string collection

Posted 23 September 2012 - 02:04 AM

I will make a few guesses on your code. You might want message box to appear only once, after for loop runs through all items in SpamFilter, not for each item. So, if you have 1000 objects in SpamFilter, you'd see 1000 message boxes. If you don't want your code to act like this, than you'll need a boolean variable outside the for loop, and assign it's value to TRUE, if any of items in SpamFilter contains value of title variable (in your case "test").

Next thing to do, is to rethink what you're making a comparison in your if clause. Now you are saying: "If value of variable title contains the value of item on index i in SpamFilter, then show message box with 'Does contain: test', if value of variable title doesn't contain the value of item on index i in SpamFilter, then show message box with 'Does not contain: test'".

If you have in your SpamFilter:
test
something else test
and again something third

... your code will act:
(i = 0) does "test" contain "test"? Yes! Show message box "Does contain : test"
(i = 1) does "test" contain "something else test"? No! Show message box "Does not contain : test"! Why not? Because a cheese doesn't contain refrigerator, but refrigerator contains cheese (and eggs, milk,...). If you'd compare the other way around, and asking if "something else test" contains "test", the answer would be yes (TRUE), and you'd see confirmation text in message box.
(i = 2) does "test" contain "and again something third"? No....

View PostFeezusChrist, on 22 September 2012 - 06:46 PM, said:

My code:


Dim title As String = "test"
        For i = 0 To My.Settings.SpamFilter.Count - 1
            If title.Contains(My.Settings.SpamFilter.Item(i).ToString) Then
                MsgBox("Does contain : " & title)

            Else

                MsgBox("Does not contain : " & title)
            End If
        Next



In the spam filter collection, one of the items is test, and the result is "does not contain : test"

Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Check if string contains any part of string collection

Posted 23 September 2012 - 06:12 AM

How about posting the XML Schema that was generated in my.setting.Spamfilter so we know exactly what is being compared. If it's a long list, put it in a code tag block or a spoiler tag block.

Your code works but has the issues that lucky3 listed and will become slow with a lot of searches the way you're doing it.

As modi123_1 asked, why are you not using the spamfilters .Contains? it will most likely be faster than you looping through your entire list for every word you want to check.

This post has been edited by CharlieMay: 23 September 2012 - 06:16 AM

Was This Post Helpful? 0
  • +
  • -

#13 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Re: Check if string contains any part of string collection

Posted 23 September 2012 - 07:58 AM

View Postlucky3, on 23 September 2012 - 02:04 AM, said:

I will make a few guesses on your code. You might want message box to appear only once, after for loop runs through all items in SpamFilter, not for each item. So, if you have 1000 objects in SpamFilter, you'd see 1000 message boxes. If you don't want your code to act like this, than you'll need a boolean variable outside the for loop, and assign it's value to TRUE, if any of items in SpamFilter contains value of title variable (in your case "test").

Next thing to do, is to rethink what you're making a comparison in your if clause. Now you are saying: "If value of variable title contains the value of item on index i in SpamFilter, then show message box with 'Does contain: test', if value of variable title doesn't contain the value of item on index i in SpamFilter, then show message box with 'Does not contain: test'".

If you have in your SpamFilter:
test
something else test
and again something third

... your code will act:
(i = 0) does "test" contain "test"? Yes! Show message box "Does contain : test"
(i = 1) does "test" contain "something else test"? No! Show message box "Does not contain : test"! Why not? Because a cheese doesn't contain refrigerator, but refrigerator contains cheese (and eggs, milk,...). If you'd compare the other way around, and asking if "something else test" contains "test", the answer would be yes (TRUE), and you'd see confirmation text in message box.
(i = 2) does "test" contain "and again something third"? No....

View PostFeezusChrist, on 22 September 2012 - 06:46 PM, said:

My code:


Dim title As String = "test"
        For i = 0 To My.Settings.SpamFilter.Count - 1
            If title.Contains(My.Settings.SpamFilter.Item(i).ToString) Then
                MsgBox("Does contain : " & title)

            Else

                MsgBox("Does not contain : " & title)
            End If
        Next



In the spam filter collection, one of the items is test, and the result is "does not contain : test"


Tried that, did not work.

Lets say we put the string title as "hey there"

and in the spamfilter, the word "hey" is an object. It is showing that it does not contain "hey".
Was This Post Helpful? 0
  • +
  • -

#14 FeezusChrist   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 30-August 12

Re: Check if string contains any part of string collection

Posted 23 September 2012 - 08:09 AM

View PostFeezusChrist, on 23 September 2012 - 07:58 AM, said:

Tried that, did not work.

Lets say we put the string title as "hey there"

and in the spamfilter, the word "hey" is an object. It is showing that it does not contain "hey".


New code that doesn't work:


 
Dim title as string = "Hey"
For i = 0 To My.Settings.SpamFilter.Count - 1
            Dim a As String = My.Settings.SpamFilter.Item(i)
            If a.Contains(title.ToString) Then
                MsgBox("Does contain " & title)
            Else
                MsgBox("Does not contain" & title)
            End If
        Next


By now when I did the breakpoint, the string a was the first item in the spamfilter.
Was This Post Helpful? 0
  • +
  • -

#15 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: Check if string contains any part of string collection

Posted 23 September 2012 - 08:30 AM

I understand that the user entry is not always one word but then it would seem that you need to take the user's input and split it into a string array of words using String.Split(" "). Then send each word found to the routine that checks to see if your stringcollection contains the word.

Another thing to keep in mind is that the word is case sensitive. So Test will not find test. My suggestion for this is to enter all words into your collection using either all upper or all lower case and then pass the .ToLower or .ToUpper version to the routine that checks the collection.

In the case of your sample above, you don't need to use a for...next loop

Get the string, if the collection.contains(the string) then respond.

EDIT: here is a demonstration
       'The following is the XML Schema that is stored in the SpamFilter Setting
        '<?xml version="1.0" encoding="utf-16"?>
        '   <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        '     <string>Hey</string>
        '     <string>able</string>
        '     <string>baker</string>
        '     <string>charlie</string>
        '     <string>dog</string>
        '     <string>test</string>
        '   </ArrayOfString>


 

        Dim title As String = "Hey"
        If My.Settings.SpamFilter.Contains(title) Then
            MsgBox("Does contain " & title)
        Else
            MsgBox("Does not contain" & title)
        End If

        'But, if you were to have
        title = "hey" 'it would not be found because of case sensitivity


This post has been edited by CharlieMay: 23 September 2012 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2