简体   繁体   中英

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#

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");

尝试这个:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM