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

前端 未结 9 2440
死守一世寂寞
死守一世寂寞 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:53

    Using String.IndexOf(String):

    str.Lenght > 0 ? "+-*/".IndexOf(str[str.Lenght - 1]) != -1 : false
    
    0 讨论(0)
  • 2020-12-08 22:00

    If you really want to, you can use De Morgan's laws to replace x || y in your code. One version says:

    !(x || y) == !x && !y
    

    If you want to have the same result, we just need to negate the entire expression twice:

    x || y == !!(x || y) == !(!x && !y)
    
    0 讨论(0)
  • 2020-12-08 22:01

    How about:-

    string input = .....;
    string[] matches = { ...... whatever ...... };
    
    foreach (string match in matches)
    {
        if (input.EndsWith(match))
            return true;
    }
    

    I know it is dreadfully old school to avoid LINQ in this context, but one day you will need to read this code. I am absolutely sure that LINQ has its uses (maybe i'll find them one day) but i am quite sure it is not meant to replace the four lines of code above.

    0 讨论(0)
提交回复
热议问题