Link to home
Start Free TrialLog in
Avatar of WPCap
WPCap

asked on

VB.NET Insert data into array.

Hi there, I have a string of names:

John,Borris,James,Chris

Now I need to insert those names into array.

Then after I need to create a loop that will search for those names in other string.
If any of those names, in any format (lowercase, uppercase) are matched then do stuff.

Thank you.
Avatar of kaufmed
kaufmed
Flag of United States of America image

I have a string of names:
Dim namesStr As String = "John,Borris,James,Chris"

Open in new window


Now I need to insert those names into array.
Dim namesArr() As String = namesStr.Split(","c)

Open in new window


Then after I need to create a loop that will search for those names in other string.
If any of those names, in any format (lowercase, uppercase) are matched then do stuff.
For i As Integer = 0 To namesArr.Length - 1
    If targetStr.IndexOf(namesArr(i), StringComparison.InvariantCultureIgnoreCase) >= 0 Then
        ' do stuff
    End If
Next

Open in new window

Avatar of WPCap
WPCap

ASKER

Something seems to be wrong with the last statement. I put some random string "tgds" and it found 3 matches however there aren't any.

It should match the name precisely. the whole name.

if string contains afoshaofiahaosifhaJOHHNYfasfh13afnfso and in the array is name JOHHNY or jOhHny then it should give one positive match.
Avatar of WPCap

ASKER

I wasn't too clear in the last message but I hope you got the point, if not then...

The data in TESTSTRING is changing every second or so. now the loop should constantly look if in TESTSTRING there is a positive match to anything that is in the array.

What I'm doing is: I have an application that captures incoming packet on specific IP.
Now when the packet arrives, for each statement should check if that packet contains any of the names in the array.
Avatar of WPCap

ASKER

I think in this case I'd need a code that would search through the packet as soon as it arrives. I will put it in onPacket event.
Can you show how you implemented the above? The logic works fine in my testing using:

targetStr = "afoshaofiahaosifhaJOHHNYfasfh13afnfso"
namesStr = "JOHHNY,Borris,James,Chris"

Open in new window


User generated image
My "do stuff" in this case was to print the word "Matched" for each match.
Avatar of WPCap

ASKER

I used textbox1.text instead of targetstr
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WPCap

ASKER

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i As Integer = 0 To bannedArr.Length - 1
            If TextBox2.Text.ToString.IndexOf(bannedArr(i), StringComparison.InvariantCultureIgnoreCase) >= 0 Then
                MessageBox.Show("match")
            End If
        Next
    End Sub
Avatar of WPCap

ASKER

yes, exactly.
Avatar of WPCap

ASKER

I typed !*(@$&SF)!$ it gave me 3 matches... I typed 9(!J(!#!JFAS , same , 3 matches.
Avatar of WPCap

ASKER

I used this:

 
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i As Integer = 0 To bannedArr.Length - 1
            If TextBox2.Text.ToString.IndexOf(bannedArr(i), StringComparison.InvariantCultureIgnoreCase) >= 0 Then
                MessageBox.Show(bannedArr(i))
            End If
        Next
    End Sub

Open in new window


and typed into the textbox something like !(#&@$NJASF and it gave me 3 matches, all of them were blank.
Avatar of WPCap

ASKER

I tried to put a match into the textbox and it gave me the matched name and also 3 empty messagebox responses.
What's in bannedArr?

If value is String.Empty, the return value is 0

http://msdn.microsoft.com/en-us/library/k8b1470s.aspx

I would modify the code as below


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        For i As Integer = 0 To bannedArr.Length - 1
          If Not String.IsNullOrWhiteSpace(bannedArr(i)) Then
            If TextBox2.Text.ToString.IndexOf(bannedArr(i), StringComparison.InvariantCultureIgnoreCase) >= 0 Then
                MessageBox.Show(bannedArr(i))
            End If
          End If
        Next
    End Sub

Open in new window

Avatar of WPCap

ASKER

Great job, man. Thanks.

Perhaps you'd want to look at my other question here:

https://www.experts-exchange.com/questions/27477687/VB-NET-HEX-to-STRING.html
Avatar of WPCap

ASKER

Before there was an option to choose assisted solution... Now it's gone I guess. It just didn't seem fair to me to accept your solution, CodeCrusier since kaufmed did most of the job, sorry.
You can "Request Attention" to have the question reopened if you'd like to select multiple comments as the answer  = )