简体   繁体   English

检查两个字符串数组中是否有相同的元素?

[英]Check if there are same elements in two string array?

I have two string arrays that hold values. 我有两个包含值的字符串数组。 How can i check if the first array contains an element that is also in the second array? 如何检查第一个数组是否包含同样位于第二个数组中的元素? I want to make a loop that checks through if there are any elements that are same in both, then i want to use that value and and display it in a message box. 我想创建一个循环来检查两者中是否有任何相同的元素,然后我想使用该值并将其显示在消息框中。 How do i compare them like that? 我如何比较那样?

string[] weekDays = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
string[] potenDays = { "Mon", "Tue", "None", "None", "None", "None", "None" };

Use Intersect 使用相交

var both =  weekDays.Intersect(potenDays);
var count = both.Count();
var daysArray = both.ToArray();
foreach (var weekDay in weekDays.Where(wd => potenDays.Contains(wd)))
{
    // Show weekDay
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM