-1

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?

Argore
  • 3
  • 2
  • 3
    No, you'll need to check each friend individually. Another way is to extract the friend name from the string (so get `bob` out of `does bob like cake?`), then see whether that's in your array -- the usual tool to do this is Regex. – canton7 Jan 13 '22 at 13:44
  • Also, e.g. `Regex.IsMatch("does bob like cake?", "^does (bob|greg|jeb) like cake\?$")` – canton7 Jan 13 '22 at 14:00
  • Did you mean a certain word, like word number 2, or just any of the words in the phrase? – Svein Terje Gaup Jan 13 '22 at 14:26

5 Answers5

4

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

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

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");
}
Pankaj
  • 2,618
  • 3
  • 25
  • 47
  • NM, looks like you addressed my concern in an edit. This seems only to check whether the indicated name is contained in the array, not whether a value in the array appears in the reference string. – paneerakbari Jan 13 '22 at 13:54
0

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");
}    
}
Girija
  • 118
  • 3
0

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);
Svein Terje Gaup
  • 1,424
  • 15
  • 29
0

Yes, there is with LINQ you can compare two arrays, any means that algo stops when it founds element of the same value.

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

Cleptus
  • 3,446
  • 4
  • 28
  • 34