Compare Ending Character to List
c#, compare, regex, string
Solution
if(goodGrades.Contains(endStr))
{
//Do something.
}
Also, this works for chars as well. And if you are trying to just compare a single character, this is a better way of doing it.
//Create a list of chars instead of strings
List<char> goodGrades = new List<char>(){'A', 'B', 'C'};
//Get the last char of your string by index
char endChar = myGrade.ToString()[myGrade.Length - 1];
//See if char is contained in list of chars
if(goodGrades.Contains(endChar))
{
//Do something
}
Problem
I am trying to compare the last character of a string to a list of known values, and if any of the known values match the end char, set a flag. I have figured out how to do it vs. one character, but I can't seem to figure out how to do it vs. a list. Can anyone help? Here is my code so far: ``` StringBuilder myGrade = new StringBuilder(); // then I pull some data, calculate some stuff, give a grade, build the string, etc. The resulting text can vary, but the grade letter will always be last. var goodGrades = new List<string> { "A", "B", "C" }; string endStr = myGrade.ToString(); endStr = endStr.Substring(Math.Max(0, endStr.Length - 1)); if (endStr == "A") //do some stuff for passing grades else //do some other stuff for failing grades ``` Again, this works perfectly for a single char...but how would I go about checking each item in the goodGrades list? Is there a regex possibility? Thanks in advance for the help.