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

Nel*_*eph 5 c# arrays

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

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

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

我怎样才能做到这一点?

Blu*_*ueM 11

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


Øyv*_*hen 6

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

arr.Where( s => !string.IsNullOrWhiteSpace(s)).ToArray();
Run Code Online (Sandbox Code Playgroud)

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

arr.Where( s => s != " ").ToArray();
Run Code Online (Sandbox Code Playgroud)