首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

摆脱C#数组中的空/空字符串值

在C#中,可以使用LINQ(Language Integrated Query)来轻松地从数组中摆脱空或空字符串值。以下是一个示例:

代码语言:csharp
复制
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        string[] array = { "Apple", "", "Orange", null, "Grape", "", "Banana", null };

        // 使用LINQ摆脱空或空字符串值
        string[] filteredArray = array.Where(s => !string.IsNullOrEmpty(s)).ToArray();

        // 输出过滤后的数组
        foreach (string s in filteredArray)
        {
            Console.WriteLine(s);
        }
    }
}

在这个示例中,我们首先创建了一个包含空值和空字符串的字符串数组。然后,我们使用LINQ的Where方法和string.IsNullOrEmpty方法来过滤数组中的空值和空字符串。最后,我们将结果转换为一个新的字符串数组,并将其输出到控制台。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券