简体   繁体   English

C# 检查字符串中的特定单词是否等于数组中的任何单词

[英]C# check if specific word in string is equal to any word in array

I'm trying to check if a certain word in a string is equal to any of the strings in an array我正在尝试检查字符串中的某个单词是否等于数组中的任何字符串

What i tried doing, which as you could tell, does not work.正如你所知道的,我尝试做的事情不起作用。

string[] friends = new string[3] {"bob", "greg", "jeb"};
if("does bob like cake?" == $"does {friends} like cake?") {
    Console.WriteLine("yes, he does");
}
else {
    Console.WriteLine("i don't know who that is");
}

Is there any way of doing this without having to loop through every string in the array?有没有办法做到这一点而不必遍历数组中的每个字符串?

One way or another, you need to check all the items until such time you get a true.一种或另一种方式,你需要检查所有的项目,直到你得到一个真实的。 For this you can use Any :为此,您可以使用Any

string[] friends = new string[3] {"bob", "greg", "jeb"};
if(friends.Any(f => "does bob like cake?" == $"does {f} like cake?")) {
    Console.WriteLine("yes, he does");
}
else {
    Console.WriteLine("i don't know who that is");
}

Live example: https://dotnetfiddle.net/4BldBo现场示例: https://dotnetfiddle.net/4BldBo

You need to check if your particular friend name is contained in the list, There is a method "Contains" for this.您需要检查您的特定朋友姓名是否包含在列表中,为此有一个方法“包含”。

string[] friends = new string[3] {"bob", "greg", "jeb"};
var friendName = "bob";
if (friends.Contains(friendName)) {
{
   Console.WriteLine("yes, he does");
}
else {
   Console.WriteLine("i don't know who that is");
}

If you know, your friend Name (bob in this case) is always the second word in the string, then you could do如果您知道,您的朋友姓名(在本例中为 bob)始终是字符串中的第二个单词,那么您可以这样做

var question = "does bob like cake?";
var questionArray = question.Split(); // Split by white space
var friendName = questionArray[1]; // Get the name of your friend
if (friends.Contains(friendName)) {
{
   Console.WriteLine("yes, he does");
}
else {
   Console.WriteLine("i don't know who that is");
}

Getting the word out of string and comparing it with other array members may help.从字符串中取出单词并将其与其他数组成员进行比较可能会有所帮助。

string[] friends = new string[3] {"bob", "greg", "jeb"};
var secondWord = str.Split(' ')[1];
foreach(var friend in friends)
{
if(secondWord == friend) {
    Console.WriteLine("yes, he does");
}
else {
    Console.WriteLine("i don't know who that is");
}    
}

Assuming that he knows which word to check since he stated "a certain word" and not "any word", then the following function might work:假设他知道要检查哪个词,因为他说的是“某个词”而不是“任何词”,那么以下 function 可能会起作用:

void CheckString(string stringToCheck, string[] friends, int indexOfWordToCheck)
{
   var delimiters = new [] {' ', '.', ',', ':', ';', '?', '!'};  
   if(friends.Contains(stringToCheck.Split(delimiters)[indexOfWordToCheck]))
   {  
      Console.WriteLine("yes, he/she does!");
   }
   else
   {
      Console.WriteLine("who is that?");
   }
}

Usage:用法:

CheckString("Does bob like cake?", new [] { "bob", "greg", "jeb" }, 1);

Yes, there is with LINQ you can compare two arrays, any means that algo stops when it founds element of the same value.是的,使用 LINQ 您可以比较两个 arrays,这意味着算法在找到相同值的元素时会停止。

string[] friends = new string[3] {"bob", "greg", "jeb"};
string str = "Does bob like cake ?";

if(friends.Intersect(str.Split(' ')).Any())
{
    Console.WriteLine("Yes");
}

Thanks to How to check if an array contains any item of another array感谢如何检查一个数组是否包含另一个数组的任何项目

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM