1

If I have a string array:

public string[] foo = {... , "ABC123", ...};

and a variable:

string bar = "ABC123DEF456";

how can I check if bar contains "ABC123"?

At the moment, I'm doing:

if (Array.Exists(foo, element => element == bar))

to check if the entire string bar exists in foo, but I want know if an element in foo is a substring of bar. How would I do this? Is it possible to use .Contains?

Gilad Green
  • 36,708
  • 7
  • 61
  • 95
Waqas
  • 43
  • 4

1 Answers1

5

Putting it into words - you want to know if any of the items in foo is contained in bar. So in code, almost exactly the same:

if(foo.Any(item => bar.Contains(item)))
Gilad Green
  • 36,708
  • 7
  • 61
  • 95