Using C# to check if string contains a string in string array
Here's how:
if(stringArray.Any(stringToCheck.Contains))
/* or a bit longer: (stringArray.Any(s => stringToCheck.Contains(s))) */
This checks if stringToCheck
contains any one of substrings from stringArray
. If you want to ensure that it contains all the substrings, change Any
to All
:
if(stringArray.All(stringToCheck.Contains))
here is how you can do it:
string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };
foreach (string x in stringArray)
{
if (stringToCheck.Contains(x))
{
// Process...
}
}
UPDATE: May be you are looking for a better solution.. refer to @Anton Gogolev's answer below which makes use of LINQ.
Try this:
No need to use LINQ
if (Array.IndexOf(array, Value) >= 0)
{
//Your stuff goes here
}