Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Array contains partial

Tags:

c#

How to find whether a string array contains some part of string? I have array like this

String[] stringArray = new [] { "[email protected]", "[email protected]", "@gmail.com" };
string str = "[email protected]"

if (stringArray.Any(x => x.Contains(str)))
{
    //this if condition is never true
}

i want to run this if block when str contains a string thats completely or part of any of array's Item.

like image 694
coure2011 Avatar asked Jul 23 '10 15:07

coure2011


1 Answers

Assuming you've got LINQ available:

bool partialMatch = stringArray.Any(x => str.Contains(x)); 

Even without LINQ it's easy:

bool partialMatch = Array.Exists(stringArray, x => str.Contains(x));

or using C# 2:

bool partialMatch = Array.Exists(stringArray,
      delegate(string x) { return str.Contains(x)); });

If you're using C# 1 then you probably have to do it the hard way :)

like image 136
Jon Skeet Avatar answered Oct 15 '22 00:10

Jon Skeet