我想使用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'是否包含数组中的一个词?
当前回答
使用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 |
stringArray.ToList().Contains(stringToCheck)
string [] lines = {"text1", "text2", "etc"};
bool bFound = lines.Any(x => x == "Your string to be searched");
如果搜索的字符串与数组'lines'中的任何元素匹配,则bFound设置为true。
使用Array类的Find或FindIndex方法:
if(Array.Find(stringArray, stringToCheck.Contains) != null)
{
}
if(Array.FindIndex(stringArray, stringToCheck.Contains) != -1)
{
}
我使用下面的代码来检查字符串是否包含字符串数组中的任何项:
foreach (string s in stringArray)
{
if (s != "")
{
if (stringToCheck.Contains(s))
{
Text = "matched";
}
}
}
演示了三个选项。我认为第三条是最简洁的。
class Program {
static void Main(string[] args) {
string req = "PUT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.A"); // IS TRUE
}
req = "XPUT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.B"); // IS TRUE
}
req = "PUTX";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.C"); // IS TRUE
}
req = "UT";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.D"); // false
}
req = "PU";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("one.1.E"); // false
}
req = "POST";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("two.1.A"); // IS TRUE
}
req = "ASD";
if ((new string[] {"PUT", "POST"}).Any(s => req.Contains(s))) {
Console.WriteLine("three.1.A"); // false
}
Console.WriteLine("-----");
req = "PUT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.A"); // IS TRUE
}
req = "XPUT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.B"); // false
}
req = "PUTX";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.C"); // false
}
req = "UT";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.D"); // false
}
req = "PU";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("one.2.E"); // false
}
req = "POST";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("two.2.A"); // IS TRUE
}
req = "ASD";
if (Array.IndexOf((new string[] {"PUT", "POST"}), req) >= 0) {
Console.WriteLine("three.2.A"); // false
}
Console.WriteLine("-----");
req = "PUT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.A"); // IS TRUE
}
req = "XPUT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.B"); // false
}
req = "PUTX";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.C"); // false
}
req = "UT";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.D"); // false
}
req = "PU";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("one.3.E"); // false
}
req = "POST";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("two.3.A"); // IS TRUE
}
req = "ASD";
if ((new string[] {"PUT", "POST"}.Contains(req))) {
Console.WriteLine("three.3.A"); // false
}
Console.ReadKey();
}
}