简体   繁体   中英

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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