Best way to check for string in comma-delimited list with .NET?

后端 未结 9 1363
没有蜡笔的小新
没有蜡笔的小新 2021-02-02 09:37

I\'m reading a comma-delimited list of strings from a config file. I need to check whether another string is in that list. For example:

\"apple,banana,cheese\"
9条回答
  •  梦谈多话
    2021-02-02 10:11

    Here's an option that ignores case.

    var food = "apple,banana,cheese";
    
    bool containsApp = food.Split(',')
                           .Where(s => string.Compare("app", s, true) == 0)
                           .Count() > 0;
    
    bool containsApple = food.Split(',')
                             .Where(s => string.Compare("apple", s, true) == 0)
                             .Count() > 0;
    
    Console.WriteLine("Contains \"app\"? {0}", containsApp);
    Console.WriteLine("Contains \"apple\"? {0}", containsApple);
    

提交回复
热议问题