在c#中查找和删除数组中的项

我有一个字符串数组。 我需要从该数组中删除一些项目。 但我不知道需要删除的项目的索引。

我的数组是:string [] arr = {“”,“a”,“b”,“”,“c”,“”,“d”,“”,“e”,“f”,“”,“ “}。

我需要删除“”项目。 即删除“”后我的结果应该是arr = {“a”,“b”,“c”,“d”,“e”,“f”}

我怎样才能做到这一点?

string[] arr = {" ", "a", "b", " ", "c", " ", "d", " ", "e", "f", " ", " "}; arr = arr.Where(s => s != " ").ToArray(); 

这将删除所有null,空或只是空格的条目:

 arr.Where( s => !string.IsNullOrWhiteSpace(s)).ToArray(); 

如果由于某种原因你只想删除像你的例子中只有一个空格的条目,你可以像这样修改它:

 arr.Where( s => s != " ").ToArray(); 

使用LinQ

 using System.Linq; string[] arr= {" ","a","b"," ","c"," ","d"," ","e","f"," "," "}. arr.Where( x => !string.IsNullOrWhiteSpace(x)).ToArray(); 

或者取决于你如何填充数组,你可以以前做

 string[] arr = stringToBeSplit.Split('/', StringSplitOptions.RemoveEmptyEntries); 

那么空条目将不会首先放入您的字符串数组中