Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何删除数组中的空元素?_C#_Arrays_Linq - Fatal编程技术网

C# 如何删除数组中的空元素?

C# 如何删除数组中的空元素?,c#,arrays,linq,C#,Arrays,Linq,我有一个电子邮件地址数组,但在数组的末尾插入了一个空字符串。如何删除数组中的此元素 for(int i = 0; i < allToAddresses.Length; i++) { if(allToAddresses[i] == " ") // find where empty element is { //Here i am trying to delete that empty element. does not work allToAddresses[i

我有一个电子邮件地址数组,但在数组的末尾插入了一个空字符串。如何删除数组中的此元素

for(int i = 0; i < allToAddresses.Length; i++)
{
    if(allToAddresses[i] == " ") // find where empty element is
    { //Here i am trying to delete that empty element. does not work
       allToAddresses[i].Split("".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
    }
}
for(int i=0;i
问题可能是您正在搜索空白而不是空字符串

请尝试以下内容:

for(int i = 0; i < allToAddresses.Length; i++)
{
   if(allToAddresses[i] == "") // find where empty element is
   { //Here i am trying to delete that empty element. does not work
      allToAddresses[i].Split("".ToCharArray(),StringSplitOptions.RemoveEmptyEntries);
   }
}
for(int i=0;i
您可以尝试使用Linq进行此操作

allToAddresses = allToAddresses.Where(address=>!string.IsNullOrWhiteSpace(address))
                               .ToArray();
您还必须在名称空间中包含以下内容:

using System.Linq;

使用
Where
方法过滤初始数组。在该方法中,如果当前地址的方法string.IsNullOrWhiteSpace返回false,则传递一个返回true的谓词。否则返回false。使用此筛选器可以丢弃空地址、空地址或仅由空白字符组成的地址

如果您使用的是数组,则需要取出有效值并将其放入数组的新实例中。您可以这样做:

internal static T[] RemoveNullArrayElements<T>(T[] array)
    {
        if (array != null)
        {
            List<T> newLst = new List<T>();
            foreach (var ar in array)
            {
                if (ar != null)
                {
                    newLst.Add(ar);
                }
            }
            return newLst.ToArray();
        }

        return array;
    }
内部静态T[]删除数组元素(T[]数组)
{
if(数组!=null)
{
List newLst=新列表();
foreach(数组中的var-ar)
{
如果(ar!=null)
{
新增(应收账款);
}
}
返回newLst.ToArray();
}
返回数组;
}
由于数组大小是固定的,因此无法真正从数组中“删除”元素*。但是,您可以构造一个跳过所有空元素的新数组:

allToAddresses = allToAddresses.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();
以上要求在文件顶部使用
System.Linq
。它检查数组中的所有条目,查看它们是否为null或完全由空格(空格、制表符等)组成,并生成一个新的字符串数组,其中仅包含原始数组中的非空/非null条目


*为了充分披露,.NET确实有,但你不应该在这种情况下使用它。

他可能需要
string.isNullorWhiteSpace(x)
,因为他的问题包括
,它不是空的,是空白。
string.IsNullOrEmpty(”不工作,因为它有空格。是的,很抱歉我看了太多,是的,在这种情况下,它必须是一个空或空白。我将编辑答案。很抱歉造成任何混乱。
。其中(!string.IsNullOrWhitespace)
将不起作用compile@haim我的错误:(感谢您发现我的输入错误
!string.IsNullOrWhiteSpace
也无法编译。错误是:“CS0023运算符”!“不能应用于“方法组”类型的操作数”如果您始终得到一个带有空字符串的最终条目,您还应该尝试查找原因,而不是修复数组。这是假设它是您的代码,或者您可以控制(团队成员的)代码。
allToAddresses = allToAddresses.Where(s => !string.IsNullOrWhiteSpace(s)).ToArray();