0

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?

0

2 Answers 2

3

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()).

Sign up to request clarification or add additional context in comments.

9 Comments

Many thanks Tim, for replying, when I add this code I receive an error: 'Except' is not a member of 'System.Array'.
@user1824963: you need to add Imports System.Linq
Hi Tim, Yes, I did add that to before adding the except code but the error persists - how strange?
I am using .Net 4.5.2
@user1824963: no, that looks wrong. You want to split the TextBox.Text so you need to use String.Split. That returns a String(). Aaah, sorry, now i see the issue. You need to declare it as String() not as Array. So this is wrong: Dim stringArr As Array = tbOldValues.Text.Split(","c) Instead use Dim stringArr As String() = tbOldValues.Text.Split(","c) or just use Dim stringArr = tbOldValues.Text.Split(","c)
|
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.