user1824963
user1824963

Reputation: 101

Remove an array from another array

I am trying to create an array from a string and splitting with a comma, and then looking to removing one array from, I have read several posts but cannot get any of these to work, so any help is really appreciated.

Dim stringArr As Array = tbOldValues.Text.Split(","c)
Dim FilterData As Array = tkbExistingNames.Text.Split(","c)

The above assigns the array, how would I achieve to remove stringArr from FilterData?

Upvotes: 0

Views: 225

Answers (2)

dbasnett
dbasnett

Reputation: 11773

I avoid arrays as much as possible, prefer List... Your problem statement is unclear, try adding examples.

With these values

    tbOldValues.Text = "two,four,six"
    tkbExistingNames.Text = "one,two,three,four,five,six,seven"

I ran this code

    Dim stringArr As List(Of String) = tbOldValues.Text.Split(","c).ToList

    Dim FilterData As List(Of String) = tkbExistingNames.Text.Split(","c).ToList

    FilterData = FilterData.Except(stringArr).ToList

FilterData had four elements at the conclusion of the code,

one
three
five
seven

Upvotes: 2

Tim Schmelter
Tim Schmelter

Reputation: 460108

Arrays have a fixed size, so there is no working Remove method. You have to create a new array. You could use LINQ which simplifies the task with Enumerable.Except:

FilterData = FilterData.Except(stringArr).ToArray()

Except returns only those items that are in the first collection but not in the second.

You also have an issue with the type of your array. Don't use Array but String(). So

Dim stringArr As String() = tbOldValues.Text.Split(","c)
Dim FilterData As String() = tkbExistingNames.Text.Split(","c)

System.Array is the old, non-generic base type of all arrays. You should almost never need it, at least when you know the type(like here String()).

Upvotes: 3

Related Questions