I need to check in string.Endswith(\"\") from any of the following operators: +,-,*,/
If I have 20 operators I don\'t want to use ||<
string s = "Hello World +";
string endChars = "+-*/";
Using a function:
private bool EndsWithAny(string s, params char[] chars)
{
foreach (char c in chars)
{
if (s.EndsWith(c.ToString()))
return true;
}
return false;
}
bool endsWithAny = EndsWithAny(s, endChars.ToCharArray()); //use an array
bool endsWithAny = EndsWithAny(s, '*', '/', '+', '-'); //or this syntax
Using LINQ:
bool endsWithAny = endChars.Contains(s.Last());
Using TrimEnd:
bool endsWithAny = s.TrimEnd(endChars.ToCharArray()).Length < s.Length;
// als possible s.TrimEnd(endChars.ToCharArray()) != s;