首页主机资讯怎么使用LINQ Intersect方法比较两个字符串数组

怎么使用LINQ Intersect方法比较两个字符串数组

时间2024-05-25 12:30:03发布访客分类主机资讯浏览431
导读:你可以使用LINQ Intersect方法来比较两个字符串数组,找出它们共同的元素。下面是一个示例: using System; using System.Linq; class Program { static void Mai...

你可以使用LINQ Intersect方法来比较两个字符串数组,找出它们共同的元素。下面是一个示例:

using System;
    
using System.Linq;


class Program
{

    static void Main()
    {

        string[] array1 = {
 "apple", "banana", "orange", "grape" }
    ;

        string[] array2 = {
 "orange", "grape", "kiwi", "pear" }
    ;
    

        var commonElements = array1.Intersect(array2);
    

        Console.WriteLine("Common elements in the two arrays:");


        foreach (var element in commonElements)
        {
    
            Console.WriteLine(element);

        }

    }

}
    

在上面的示例中,我们定义了两个字符串数组array1array2,然后使用LINQ的Intersect方法找出这两个数组的共同元素。最后,我们遍历输出共同元素的值。运行该程序,输出将会是:

Common elements in the two arrays:
orange
grape

这表明array1array2中共同的元素是”orange”和”grape”。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: 怎么使用LINQ Intersect方法比较两个字符串数组
本文地址: https://pptw.com/jishu/667748.html
合理的命名div 单元格出现#REF!

游客 回复需填写必要信息