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?

1

1 Answer 1

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)))
Sign up to request clarification or add additional context in comments.

1 Comment

Quick and simple response. Thanks!

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.