Issue
I want to use C# to check if a string value contains a word in a string array. For example,
string stringToCheck = "text1text2text3";
string[] stringArray = { "text1", "someothertext", etc... };
if(stringToCheck.contains stringArray) //one of the items?
{
}
How can I check if the string value for ‘stringToCheck’ contains a word in the array?
Solution
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.
Answered By – Abdel Raoof Olakara
Answer Checked By – Marilyn (BugsFixing Volunteer)