首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么把数组中的空值去掉

2012-01-03 
如何把数组中的空值去掉现有一组数组myWords[],其中有些值是空值,我如何将这myWords[]中空值去掉,把剩下的

如何把数组中的空值去掉
现有一组数组myWords[],其中有些值是空值,我如何将这myWords[]中空值去掉,把剩下的非空值留下组成另一组数组,应该怎么做,请指点

[解决办法]

C# code
    List<string> strs = new List<string>();    foreach (string s in myWords)    {        if(! string.IsNullOrEmpty(s) ) strs.Add(s);    }    string[] result = strs.ToArray();
[解决办法]
string[] ss = new string[5] { "ss","55",string.Empty,"ee",string.Empty};
foreach(string s in ss)
{
ArrayList h = new ArrayList();
if (s != string.Empty)
{
h.Add(s);
foreach (string m in h)
{
MessageBox.Show(m);
}
 
}
}
仅供参考!
[解决办法]
List<string> strs = new List<string>();
foreach (string s in myWords)
{
if(! string.IsNullOrEmpty(s) ) strs.Add(s);
}
string[] result = strs.ToArray();

这样就行

热点排行