Home > database >  C# check if specific word in string is equal to any word in array
C# check if specific word in string is equal to any word in array

Time:01-13

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?

CodePudding user response:

One way or another, you need to check all the items until such time you get a true. For this you can use 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

CodePudding user response:

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

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");
}

CodePudding user response:

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");
}    
}

CodePudding user response:

Assuming that he knows which word to check since he stated "a certain word" and not "any word", then the following function might work:

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);

CodePudding user response:

If any word of the question can be the frind's name, I would:

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

string[] questionWords = question.Split(' ');

var matchingNames = friends.Intersect(questionWords);
  •  Tags:  
  • Related