I have this:
if (input.Text.ToUpper() == \"STOP\")
But there are so many possible values that I wouldn\'t be able to specify them all sepa
You can hold an array of the possible words and match it with the Contains
method:
string[] validInput = new string[] { "STOP", "END", "NO", "YES" };
// input is the input you have
if (validInput.Contains(input.Text.ToUpper()))
{
// Do something
}
var tasks = new List<string> { "STOP", "END", "NO", "YES" };
tasks.Contains(input.Text.ToUpper());
looks better
var tasks = new List<string> { "stop", "end", "no", "yes" };
tasks.Exists(x => string.Equals(x, input.Text, StringComparison.OrdinalIgnoreCase));
You could use LINQ to do this. That way if you have both STOP and END in your string, it will pick up on both of them:
var myStringArray= new[]{ "STOP", "END", "NO", "YES" };
var query = myString.Any(x => myStringArray.Contains(x));
Perfect situation for a string extension
Add this to a new file
namespace appUtils
{
public static class StringExtensions
{
public static bool In(this string s, params string[] values)
{
return values.Any(x => x.Equals(s));
}
}
}
and call from your code in this way
if(input.Text.In("STOP", "END", "NO", "YES") == true)
// ... do your stuff
create extension:
public static class Extension
{
public static bool EqualsAny(this string item, params string[] array)
{
return array.Any(s => item.ToUpper() == s.ToUpper());
}
}
using:
if (inputText.EqualsAny("STOP", "END", "NO", "YES"))
You could use a collection like array and Enumerable.Contains:
var words = new[]{ "STOP", "END", "NO", "YES" };
if(words.Contains(input.Text.ToUpper()))
{
// ...
}