我想使用c#检查字符串值是否包含字符串数组中的单词。例如,

string stringToCheck = "text1text2text3";

string[] stringArray = { "text1", "someothertext", etc... };

if(stringToCheck.contains stringArray) //one of the items?
{

}

我如何检查字符串值'stringToCheck'是否包含数组中的一个词?


当前回答

⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

只需使用LINQ方法:

stringArray.Contains(stringToCheck)

其他回答

LINQ:

arrray。Any(x => word.Equals(x));

这是为了查看数组是否包含单词(精确匹配)。使用. contains作为子字符串,或者其他任何你可能需要应用的逻辑。

使用LINQ和方法组将是最快和更紧凑的方法。

var arrayA = new[] {"element1", "element2"};
var arrayB = new[] {"element2", "element3"};

if (arrayB.Any(arrayA.Contains)) 
    return true;
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

试试这个:

不需要使用LINQ

if (Array.IndexOf(array, Value) >= 0)
{
    //Your stuff goes here
}
⚠️ Note: this does not answer the question asked
The question asked is "how can I check if a sentence contains any word from a list of words?"
This answer checks if a list of words contains one particular word

我在控制台应用程序中使用以下命令检查参数

var sendmail = args.Any( o => o.ToLower() == "/sendmail=true");

使用Array类的Find或FindIndex方法:

if(Array.Find(stringArray, stringToCheck.Contains) != null) 
{ 
}
if(Array.FindIndex(stringArray, stringToCheck.Contains) != -1) 
{ 
}