How to use string.Endswith to test for multiple endings?

前端 未结 9 2467
死守一世寂寞
死守一世寂寞 2020-12-08 21:17

I need to check in string.Endswith(\"\") from any of the following operators: +,-,*,/

If I have 20 operators I don\'t want to use ||<

9条回答
  •  粉色の甜心
    2020-12-08 21:34

    Test the last char of the string using String.IndexOfAny(Char[], Int32) method (assuming str is your variable):

    str.IndexOfAny(new char[] {'+', '-', '*', '/'}, str.Length - 1)
    

    Complete expression:

    str.Lenght > 0 ? str.IndexOfAny(new char[] {'+', '-', '*', '/'}, str.Length - 1) != -1 : false
    

提交回复
热议问题