0

I have a method which recieves an array of strings from another method. This array contains several strings, for days, moviegenres etc. I need to check if the array contains any of the moviegenres, and if so I need that specific value.

here is what I have now:

if (eventsSelected.Contains("act") ||
               eventsSelected.Contains("adv") ||
               eventsSelected.Contains("ani") ||
               eventsSelected.Contains("doc") ||
               eventsSelected.Contains("dra") ||
               eventsSelected.Contains("hor") ||
               eventsSelected.Contains("mys") ||
               eventsSelected.Contains("rom") ||
               eventsSelected.Contains("sci") ||
               eventsSelected.Contains("thr"))
            {
                //get the value that is in the array contains.
            }

Because my code checks for 10 different values how can I find out which value is true?

So let's say the array contains the value "act" how can I get that specific value?

0

5 Answers 5

3
foreach(var match in new [] {"act", "adv"}.Where(eventsSelected.Contains))
{
    //do stuff
}

Or if you just need the first one

var match = Array.Find(new[] { "act", "adv" }, eventsSelected.Contains);
if (!string.IsNullOrEmpty(match))
{
    // If you just want the first match
}

The match field contains the token you were searching on, so act or adv.

Sign up to request clarification or add additional context in comments.

Comments

3

Either use multiple if or use a string[] and Array.FindIndex:

string[] tokens = {"adv", "ani", "doc" .... };
int index = Array.FindIndex(tokens, t => eventsSelected.Contains(t));
if(index >= 0)
{
     Console.WriteLine("First value found was: " + tokens[index])
}

2 Comments

If you wanna use the static Array methods, you can use the Array.Find, since the index value is never really used.
@YuriyFaktorovich: well, it has one advantage, you can use Array.FindIndex(array, startIndex, predicate) if you want to find the next match. You just need to pass ++index.
0

You can use Intersect

var values = new[] {"act", "adv", "ani", etc};
var matches = values.Intersect(eventsSelected);

//matches contains all matching values

4 Comments

This does an equality comparison, not a Contains like OP does.
@PatrickHofman Actually, I'm not sure it does. If eventsSelected is an array (and the name suggests it is), then Contains is testing equality for each element of the array.
Agreed. Code is ambiguous.
eventsSelected is indeed an array, I realise I should have made that more clear.
0

Similar to the answer from Tim, but using LINQ rather than Array functions:

string[] tokens = {"adv", "ani", "doc" .... };
string firstMatch = eventsSelected.FirstOrDefault(s => tokens.Contains(s));
if (firstMatch != null)
{
    // Do something with firstMatch
}

Comments

0

I couldn't help but notice that you've written your tokens in sorted order so just for the fun of it you could do:

string[] sortedTokens = {"act", "adv", "ani", ... };
int index = Array.FindIndex(eventsSelected, e => Array.BinSearch(sortedTokens, e) > 0)

And then proceed as in Tim's answer, except that this time, index is the index from eventsSelected. This gives you O(nlog(k)) time complexity, where n is the size of eventsSelected and k is the size of tokens (other answers give O(nk), not that it matters much).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.