Simplify multiple if statements that use if (string.contains())

后端 未结 3 1626
青春惊慌失措
青春惊慌失措 2021-01-26 10:20

I am working on a personal assistant program and I have a method called input_parse() which looks at the input string and checks for words that correspond to \"commands\" the ps

3条回答
  •  死守一世寂寞
    2021-01-26 10:51

    You can use IEnumerable.Any (for "or" conditions") or IEnumerable.All (for "and" conditions):

    if (input.contains("argA"))
       {//execute command A}
    if (new[] { "argB", "argC" }.All(input.Contains))
       {//execute command B}
    

提交回复
热议问题