How to extract matching strings from two string arrays (C#.net):
For example: You have the following two string arrays and you want extract the matching strings from both. Then through LINQ you can do this: In this particular example “is”, “bad” and “boy” are same in both strings.
string[] str1 = new String[] { "osama", "is", "bad", "boy" };
string[] str2 = new String[] { "is", "awesome", "bad", "boy" };
Code
In the query: what we are doing is this: matching each string in str1 on str2.
List<string> lst =
(from String s in str1
where str2.Contains(s)
select s).ToList();
Displaying the list:
foreach (var item in lst)
Console.WriteLine(item);
Output of the program is:
is
bad
boy
0 comments:
Post a Comment