Ответы до сих пор подразумевают, что вы ищете слов , но из вашего вопроса я понимаю, что вы ищете для проверки на возможные сложные условия. Если вы хотите проверить простые точные совпадения, используйте массив строк для хранения ваших условий, а затем переберите массив, используя IndexOf. Например ...
string[] myTests = { "I", "am", "Steven", "King" };
string data = "I am Steven";
Dictionary<int, int> testResults = new Dictionary<int,int>();
for (int idx = 0; idx < myTests.Length; ++idx)
{
int strIndex = data.IndexOf(myTests[idx], StringComparison.InvariantCultureIgnoreCase);
testResults[idx] = strIndex;
}
// Each test index in the results dictionary that is >= 0 is a match, -1 is a failed match
Но если у вас сложные требования, вы можете рассмотреть возможность использования регулярных выражений. Вот одна из идей, но без четкого понимания того, что вы пытаетесь достичь, она может быть вам не подходит.
//Regex example
string pattern = @"(I|am|Steven)(?<=\b\w+\b)";
string data2 = "I am Steven";
MatchCollection mx = Regex.Matches(data2, pattern);
foreach (Match m in mx)
{
Console.WriteLine("{0} {1} {2}", m.Value, m.Index, m.Length);
}
string negativePattern = @"^.*(?!King).*$";
MatchCollection mx2 = Regex.Matches(data2, negativePattern);
if (mx2.Count != 1)
Console.WriteLine("Contains a negative match.");