有没有任何方法可以从数组中删除空索引,例如
string[] test={"1","","2","","3"};
Run Code Online (Sandbox Code Playgroud)
在这种情况下有任何metod可用于从数组中使用c#删除空白索引我希望得到这种格式的数组,这
test={"1","2","3"};意味着从数组中删除两个索引并最终得到3个索引我没有得到确切的代码数组这是我想做的提示
xan*_*ded 164
如果您使用的是.NET 3.5+,则可以使用linq.
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();
Chr*_*Wue 30
如果您使用的是.NET 3.5或更高版本,则可以使用Linq:
test = test.Where(x => !string.IsNullOrEmpty(x)).ToArray();
Run Code Online (Sandbox Code Playgroud)
如果你不能使用Linq那么你可以这样做:
var temp = new List<string>();
foreach (var s in test)
{
if (!string.IsNullOrEmpty(s))
temp.Add(s);
}
test = temp.ToArray();
Run Code Online (Sandbox Code Playgroud)
我编写下面的代码来删除数组字符串中的空白值。
string[] test={"1","","2","","3"};
test= test.Except(new List<string> { string.Empty }).ToArray();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
108850 次 |
| 最近记录: |