How would i go about making a function that would return true if a string contains any element of an array?
Something like this:
string str = "hi how are you";
string[] words = {"hi", "hey", "hello"};
would return true.
You can do it like this:
var array = new[] {"quick", "brown", "fox"};
var myString = "I love foxes.";
if (array.Any(s => myStr.IndexOf(s) >= 0)) {
// One of the elements is there
}
This approach does not require that the element be a full word (i.e. the above fragment would return true, even though the word "fox" is not present there as a single word).
if (array.Any(s => myStr.Contains(s)) { will do as well, I believe.myStr.IndexOf(s, StringComparison.OrdinalIgnoreCase) to make it case-insensitive.bool b = words.Any(str.Contains);You can split a string and use Enumerable.Intersect. It will be much more efficient for long strings than Any + IndexOf:
var any = words.Intersect(str.Split()).Any();
false - that might be what you're looking for, but if it is then you should probably change your question.
foreach+String.IndexOf