Remove an object from an array of string

how to remove a string from an array of string

@abdel
If you want to delete the element based on value then use

Array_Variable.Remove(“element”)

If you want delete based on index then use
Array_Variable.RemoveAt(index)

Regards,
Mahesh

there is no remove methode for arrays

You can convert it to list using array.toList, then it will have the given methods but effectively that should affect your array

Also this can be achieved using linq so you can using inside an assign (other method are subs and require to use either invoke code or invoke method

strArray = strArray.Where(Function(s) s = “toberemove”).ToArray

Cheers

5 Likes

i had used text2.ToList.RemoveAt(0)

but it say that value does not reproduce a value

Go for the second approach i provided - As said this require to use either invoke method/Code and would only work if you have a list initially.

You cannot remove items from an array, you can only reassign it to a new one.

Cheers

1 Like

there is a message:

You need to select the type of the list with the dropdown

image

Cheers

1 Like

I agree with @Florent_Salendres’s solution. You can just basically filter your array to remove the item.
But I think he meant <>, not =

Assign activity: strArray = strArray.Where(Function(s) s <> “toberemove”).ToArray

So you only pull in all items that don’t equal the string.

11 Likes

Indeed :slight_smile:

2 Likes

@ClaytonM,

This syntax is used to delete all the occurrence of the string in the array…

I have the index of the array which is needed to be deleted…

How can I delete particular index in array?

Hi.

Normally, I use a LINQ method to take all items that meet a condition, but this will remove all items that have the same value, which may not be what you want.

LINQ example:

For each item In itemArray
    newArray = itemArray.Where(Function(i) i <> item).ToArray

where “item” represents each item within the array during a loop… no index needed. You can use an index instead of “item” if you want, but I’m assuming you are running through the array in a loop.

Another way to remove elements in an array is to do it as a List, and use the .Remove() or .RemoveAt()

Example:

itemArray.ToList.RemoveAt(index).ToArray

However, I have not double checked to see if I did that right.

For more ideas on these solutions, refer here:
https://www.google.com/search?q=remove+index+from+array+.net&rlz=1C1GCEJ_enUS862US862&oq=remove+index+from+array+.net&aqs=chrome..69i57.13042j0j1&sourceid=chrome&ie=UTF-8

Regards.

1 Like

It shows blue mark error like,

Expression does not produce a value

1 Like

I think you need to use Invoke Method instead for RemoveAt

Here is a quick sample:

image

I assigned the array to a generic List type variable. To convert it back to an array, use .ToArray, but you could just use a List instead if you decide to.

Regards.

5 Likes

Thank you @ClaytonM … It works!

1 Like