C# check array item is in a string

Checking string in array is simple


string[] array = { "foo", "bar" };
if (array.Contains("foo")) {
    //...
}


I need to check if array item(s) is in string. Is it possible to avoid a loop here?


string[] array = { "foo", "bar" };
 string item = "foolish";
bool isIitem = item.Contains(array); //looking for some thing similar

Thanks

there are two ways this can be done, the first is with a loop, the second is using LINQ’s “Any” extension method with 1 line of code and a lambda expression:


string[] haystack = {"hello", "world"};
string needle = "he";
	
bool isFound = false;
foreach (var bale in haystack)
{
	if(bale.Contains(needle))
		isFound = true;
}
	
bool doesAnyItemContainThisString = haystack.Any(bale => bale.Contains(needle));

Ah, Lambda expressions and LINQ. Is there anything more beautiful on this planet? I think not,

I couldn’t agree more. Despite the ASP.NET + WebForms paradigm having rubbed web developers up the wrong way for quite awhile I think the ASP.NET Web Pages WebMatrix, Razor and .NET 3.5/4.0 and language developments (LINQ, var, automatic properties, automatic properties, collection intializers, [URL=“http://msdn.microsoft.com/en-us/library/dd264739.aspx”]named and optional arguments, etc) greatly increase the efficiency and elegance of web development in .NET


List<string> blahList = new List<string>(
new string[] { "foo", "bar"});
string item = "foolish";
bool isIitem = blahList.Contains(item);

goldensum’s post made me re-read the question (sorry!). I wrote it in reverse. I think what he was trying to say is “does the string contain any of the items in the array”. Therefore the needles and the haystack are reversed. Here are the two options:


string[] needles = {"hello", "world"};
string haystack = "hello world";
bool doesHaystackContainAnyNeedles = false;

// LINQ
doesHaystackContainAnyNeedles = needles.Any (needle => haystack.Contains(needle));

// Loop
foreach(var needle in needles)
{
	if(haystack.Contains(needle))
		doesHaystackContainAnyNeedles = true;
}

In cases like this, the linq bit will still be doing a loop under the covers – how else can it find the match where there is no index or other way of cheating?

Yes, of course. But somehow I still prefer the elegance of LINQ and though this is a simple example I find it pays dividends in other areas where one ends up using a cleaner more functional programming style rather than mutating variables.

To take it to the extreme, I occasionally use the .ForEach(x => DoSomething(x)) extension on my List<T> rather than a foreach(var x in y) { DoSomething(x); } I note ReSharper seems to prefer this too). Then when you want to parallelize it, it’s as simple as using the Parallel.ForEach method rather than the ForEach extension method. If I want to do a transformation of the data beforehand, I can just use a .Select(x => x) extension method in front of the .ForEach().

thanks.

No arguments there. Just wanted to dispel the idea that linq was some sort of database like magic that could make straight list lookups faster than O(n) operations . . .