Reputation: 8424
I need to check if a string pattern in the array is contained in a string. I am using the following code but it matches the exact string contained in the array not the pattern so the following will fail. How can I do this?
String[] stringArrayToBlock = { "#", "/", "string1:", "string2" };
String SearchString = "String1:sdfsfsdf";
if (stringArrayToBlock.Contains(SearchString.Trim().ToLower()))
{
//Do work
}
Upvotes: 0
Views: 146
Reputation: 21917
Use the LINQ Any()
method to determine if any of the elements of the array satisfy the condition.
The Contains
method used here is that of string
, not of Array
.
String[] stringArrayToBlock = { "#", "/", "string1:", "string2" };
String SearchString = "String1:sdfsfsdf";
if (stringArrayToBlock.Any(s => SearchString.Trim().ToLower().Contains(s)))
{
//Do work
}
Upvotes: 3
Reputation: 18534
I guess you should do it vice versa. Besides, if you're open to LINQ, Enumerable.Any Method is very handy:
string[] stringArrayToBlock = { "#", "/", "string1:", "string2" };
string SearchString = "String1:sdfsfsdf";
string lowerCaseString = SearchString.Trim().ToLower();
if (stringArrayToBlock.Any(s => lowerCaseString.Contains(s)))
{
//Do work
}
Upvotes: 3