我想使用c#检查字符串值是否包含字符串数组中的单词。例如,
string stringToCheck = "text1text2text3";
string[] stringArray = { "text1", "someothertext", etc... };
if(stringToCheck.contains stringArray) //one of the items?
{
}
我如何检查字符串值'stringToCheck'是否包含数组中的一个词?
我想使用c#检查字符串值是否包含字符串数组中的单词。例如,
string stringToCheck = "text1text2text3";
string[] stringArray = { "text1", "someothertext", etc... };
if(stringToCheck.contains stringArray) //one of the items?
{
}
我如何检查字符串值'stringToCheck'是否包含数组中的一个词?
当前回答
试试这个。示例:检查字段是否包含数组中的任何单词。检查字段(someField)是否包含数组中的任何单词。
String[] val = { "helloword1", "orange", "grape", "pear" };
Expression<Func<Item, bool>> someFieldFilter = i => true;
someFieldFilter = i => val.Any(s => i.someField.Contains(s));
其他回答
使用Array类的Find或FindIndex方法:
if(Array.Find(stringArray, stringToCheck.Contains) != null)
{
}
if(Array.FindIndex(stringArray, stringToCheck.Contains) != -1)
{
}
⚠️ 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)
你也可以像Anton Gogolev建议的那样,检查stringArray1中的任何项是否与stringArray2中的任何项匹配:
using System.Linq;
if(stringArray1.Any(stringArray2.Contains))
同样,stringArray1中的所有项都匹配stringArray2中的所有项:
using System.Linq;
if(stringArray1.All(stringArray2.Contains))
⚠️ 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 |
stringArray.ToList().Contains(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
if (Array.IndexOf(array, Value) >= 0)
{
//Your stuff goes here
}