Just like in the title. I got one array of strings and second array of strings. I want to display result in this kind of pattern: first element of the first array – then all elements from second array that occurs in first element of first array. After that second element of first array and all elements from second array that occurs in second element of first array. And so on.
For example:
string[] arrayA = {"Lorem ipsum dolor sit amet, justo", "notgin like good cold beer"};
string[] arrayB = {"justo","beer","lorem"}
for (int i = 0; i < arrayA.Length; i++)
{
Console.WriteLine(arrayA[i]);
for (int j = 0; j < arrayB.Length; j++)
{
int controlIndex = arrayA[i].IndexOf(arrayB[j]);
if (controlIndex != -1)
{
Console.Write(" :--contains-->" + arrayB[j]);
}
}
}
So the result should looks like this:
- Lorem ipsum dolor sit amet, justo :–contains–> justo,lorem
- notgin like good cold beer :–contains–> beer.
But mine result is:
– Lorem ipsum dolor sit amet, justo :–contains–> justo
– notgin like good cold beer :–contains–> beer.
So as you can see there is no lorem listed
This is not hard at all if you break your problem down some. First of all, get away from dealing with arrays and indexes into them. Just use
IEnumerable<T>, it will make your life easier.Here’s how I see it:
First, you want to find all strings from an array
needles, that are part of a string,haystack.This will return an IEnumerable of all of the strings from
needlesthat are a part ofhaystack.Then, you want to simply iterate over all of your search strings, I’ll call that
haystacks.Note that
String.Contains()is case-sensitive. So “Lorem” will not match “lorem”. If you want this behavior, you will have to convert them to lowercase first.