I'm newbie in VB.NET

  • Thread starter Thread starter Archil Phanchulidze
  • Start date Start date
A

Archil Phanchulidze

I'm newbie in VB.NET



I have two string arrays A and B



Dim A() as String = {"A","B","C"}

Dim B() As String = {"C","D","E"}



I need to find in A() array elements which contains B() array and remove
it.



for example I need to find string C in A array and remove it.



I'm now using BinarySearch method and Array.Clear() but seems I do
something wrong.



Is any other way ?



Can anybody help me ?
 
I'm sure someone can come up with a tighter algorithm (I have a nagging
suspicion that either Sort or a Hashtable is involved), but this gets the
job done.

Bob
------------------

Module Main

Public Sub main()
Dim A() As String = {"A", "B", "C"}
Dim B() As String = {"C", "D", "E"}
Dim c() As String = Exclude(A, B)
DisplayArrayContents(c)
End Sub

Private Function Exclude(ByVal Source() As String, ByVal Remove() As
String) As String()
Dim ret(Source.Length) As String
Dim i As Integer = -1
For Each s As String In Source
If System.Array.IndexOf(Remove, s) < 0 Then
i += 1
ret(i) = s
End If
Next
ReDim Preserve ret(i)
Return ret
End Function

Private Sub DisplayArrayContents(ByVal array() As String)
Dim str As String
For Each s As String In array
str &= s & ", "
Next
str = str.TrimEnd(", ".ToCharArray)
MsgBox(str)
End Sub

End Module
 
Bob,

Nice you did this, you did probably not know it, however we try in this
newsgroup to avoid doing homework for students and this looks in my opinion
very much to it.

Just for the next time.

Cor

"Bob"
 
Back
Top