We help IT Professionals succeed at work.

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.
Comment
Watch Question

Most Valuable Expert 2011
Top Expert 2015

Commented:
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

Author

Commented:
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.

Author

Commented:
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.

Author

Commented:
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.
Most Valuable Expert 2011
Top Expert 2015

Commented:
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


Screenshot
My "do stuff" in this case was to print the word "Matched" for each match.

Author

Commented:
I used textbox1.text instead of targetstr
Most Valuable Expert 2011
Top Expert 2015
Commented:
That shouldn't matter. "textbox.text" is of type String; so is "targetstr".

Author

Commented:
   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

Author

Commented:
yes, exactly.

Author

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

Author

Commented:
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.

Author

Commented:
I tried to put a match into the textbox and it gave me the matched name and also 3 empty messagebox responses.
Most Valuable Expert 2012
Top Expert 2014

Commented:
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

Author

Commented:
Great job, man. Thanks.

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

http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_27477687.html

Author

Commented:
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.
Most Valuable Expert 2011
Top Expert 2015

Commented:
You can "Request Attention" to have the question reopened if you'd like to select multiple comments as the answer  = )