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.