How can I compare each element of one string array to every element of another string array?
11,383
Solution 1
foreach (string str in yourFirstArray)
{
if (yourSearchedArray.Contains(str))
{
Console.WriteLine("Exists");
}
}
Solution 2
foreach (string str in strArray)
{
foreach (string str2 in strArray2)
{
if (str == str2)
{
Console.WriteLine("element exists");
}
}
}
Updated to display when string does not exist in strArray2
bool matchFound = false;
foreach (string str in strArray)
{
foreach (string str2 in strArray2)
{
if (str == str2)
{
matchFound = true;
Console.WriteLine("a match has been found");
}
}
if (matchFound == false)
{
Console.WriteLine("no match found");
}
}
Or another way of doing this in less lines of code:
foreach (string str in strArray)
{
if(strArray2.Contains(str))
{
Console.WriteLine("a match has been found");
}
else
{
Console.WriteLine("no match found");
}
}
Author by
born2fr4g
Updated on June 04, 2022Comments
-
born2fr4g almost 2 years
I've got two string arrays. I want to select one element from the first array and compare to each element of the second array. If element from first array exist in elements of second array i whant to write for example ("Element exist") or something like this.
This should be possible to do with two for loops ?
EDIT
Ok i finaly achived what i wanted usign this code:
string[] ArrayA = { "dog", "cat", "test", "ultra", "czkaka", "laka","kate" }; string[] ArrayB = { "what", "car", "test", "laka","laska","kate" }; bool foundSwith = false; for (int i = 0; i < ArrayA.Length; i++) { for (int j = 0; j < ArrayB.Length; j++) { if (ArrayA[i].Equals(ArrayB[j])) { foundSwith = true; Console.WriteLine("arrayA element: " + ArrayA[i] + " was FOUND in arrayB"); } } if (foundSwith == false) { Console.WriteLine("arrayA element: " + ArrayA[i] + " was NOT found in arrayB"); } foundSwith = false; }
I hope this will help others who will want to compare two arrays ;). its all about this foundSwitch. Thx for help once again.
-
Sergey Kalinichenko almost 12 years"This should be possible to do with two for loops ?" Absolutely! Did you try writing these two loops? How did it go?
-
Glenn Ferrie almost 12 yearsIt probably takes longer to write this post than to write a script to test this. #lazy
-
born2fr4g almost 12 yearsSorry about not giving a code. I edited first post with my code.
-
eyossi almost 12 yearsYou can replace
else if (s1 != s2)
with onlyelse
... and it will work fine... what is the problem then? -
born2fr4g almost 12 yearsCould you check out my example in main question ? I just added to clarify what im trying to do. When im using else instead of else if i got the same wrong result (just like i write in example in main question)
-
-
Chris Taylor almost 12 yearsThis is a good solution, but you should not create the List<> it creates a new copy of the array which is unnecessary overhead. You could just use yourStringArray.Contains(str) directly.
-
born2fr4g almost 12 yearsIm using .NET framework 2.0 and o got errors with this Even when i add using System.Linq.
-
born2fr4g almost 12 yearsIts good but when i add to this if and else statmant for displaying this element not exist i receive weird result. Like its over iterating. How to add to this functionality - when element exists it writes element exists but when not element does not exists ?
-
eyossi almost 12 yearscan you try using 'Array.Exists()` instead of
Contains
? (msdn.microsoft.com/en-us/library/yw84x8be(v=vs.80)) -
born2fr4g almost 12 yearsi cannot unfortunately. Im trying to use code from belowe comment: foreach (string str in strArray) { foreach (string str2 in strArray2) { if (str == str2) { Console.WriteLine("element exists"); }else { Console.WriteLine ("element doesnt exists"); } } But i also need else statmant to write (this element does not exists). But when i write else stataments it overiterates i dont know why
-
eyossi almost 12 yearscan you update your code with the new version and explain the problem again?
-
born2fr4g almost 12 yearsI updated the code in above. Hope this will help you to help me ;)