-1

Yes I'm a beginner and maybe it is not possible, but is there anything I can do to test if a string is equal to a string array?

        string speech = e.Result.Text;
        String[] greeting = new string[2] { "hello", "hi" };

            if (speech == greeting) 
            {
                James.SpeakAsync("Hi");
            }
onas
  • 11

5 Answers5

0

So you got a string array of 2 names

string[] arrayOfNames { "Jon", "Jack" }
string jon = "Jon";

/*In order to check if the name you have in your var is same as in the list you can do a foreach loop*/

foreach(var name in arrayOfNames)
{
//Check each key if we have name with jon
    if (name == jon) Print("Succes");
}

Caderikor
  • 11
  • 2
0

Linq extension methods is best way for this.

using System.Linq;
...
if(greeting.Contains(speech) {
   // Do something
}
Mustafa Arslan
  • 774
  • 5
  • 13
0

Method: Linq

If you already have Linq imported, then you can just use Linq:

string speech = e.Result.Text;
string[] greeting = new string[2] { "hello", "hi" };
if (greeting.Contains(speech))
{
     James.SpeakAsync("Hi");
}

Method: IndexOf

If you aren't already using Linq, check the index of the string using Array.IndexOf. The result will be -1 if there is no match.

string speech = e.Result.Text;
string[] greeting = new string[2] { "hello", "hi" };
if (Array.IndexOf(greeting, speech) > -1)
{
     James.SpeakAsync("Hi");
}
Mark Roach
  • 1,039
  • 9
  • 20
0

You can use LINQ methods: Contains() or Any().

You can check if the array contains the given string as follows:

string speech = e.Result.Text;
        var greeting = new string { "hello", "hi" };

            if (greeting.Contains(speech)) 
            {
                James.SpeakAsync("Hi");
            }

Or

string speech = e.Result.Text;
        var greeting = new string { "hello", "hi" };

            if (greeting.Any(g=>g.Equals(speech))) 
            {
                James.SpeakAsync("Hi");
            }

0

I will assume you have some string speech and you want to know if that string is equal to a string in your array greeting. (I based this assumption off the fact you want to accept some speech and equate it to a variety of greetings - what you are calling "equality" here is really that the speech belongs to a set of greetings.)

In that case you will need to loop over the array values and compare them one-by-one with your original string. I suggest using a for loop.

For example, let's assume speech is "hi" here:

    string speech = "hi";
    String[] greeting = new string[2] { "hello", "hi" };

    for (int i = 0; i < greeting.Length; i++) {
      if (speech == greeting[i]) 
      {
          Console.WriteLine ("found string in array");
      }
    }

Notice I use greeting.Length to get the size of the array (which is 2). (See: Array.Length.)

Also notice that I use greeting[i] to access the ith string of the array.

i starts at 0, so the first string in the array is fetched with greeting[0] gives you the first element of the array, "hello" in this case.

"hello" does not equal "hi" so nothing happens, and the loop starts again with i = 1 instead of i = 0.

With i = 1 now, greeting[1] gives us "hi", which is the same as speech, and we get the console message I wrote.

Is this what you were looking for?

Since you're a beginner, I would advise spending some time reading the docs on Arrays: https://learn.microsoft.com/en-us/dotnet/api/system.array

Be sure to experiment with examples to get yourself familiar.

Once you are comfortable with loops, a more elegant solution would be to use Array.IndexOf, as mentioned in this answer to a similar question.