Hiba
Hiba

Reputation: 251

How to remove item from string array in C#?

I have an array of string . I want to remove an item from string. HOw i can do this.

string []values = User.Split(';');

Suppose values contains "1","2","3","4"

I want to delete or remove item "2" from values. How i can do this. Is there built in function in C#

Upvotes: 2

Views: 32120

Answers (2)

Syllith
Syllith

Reputation: 317

Try this:

myArray = myArray.Where(w => w != myArray[2]).ToArray(); 

Upvotes: 1

Backs
Backs

Reputation: 24923

Array is immutable object. So, you can't remove from array. You can create new array without this value using LINQ:

values = values.Where(o=> o != "2").ToArray();

Or, you can create List and remove from list:

List<string> values = User.Split(';').ToList();
values.Remove("2");

Upvotes: 9

Related Questions