确定产品名称中是否存在符号

时间:2015-04-06 05:56:38

标签: c# .net

我有这个需要识别的产品列表,它有模式符号。到目前为止,下面是我用来识别它们的代码。不幸的是,当我调试程序时,它并没有识别出模式。

            string[] patterns = new string[] { "." ,",", ";", "'", "`", ":", "/", "\\", "*", "|" ,"(", ")","[", "]", "{", "}", "<",  ">" };

            List<Product> products = new List<Product>
            {
                new Product { ID = 1, Name = "Jenny's Product", Price = 10 },
                new Product { ID = 2, Name = "Brunus C.O.", Price = 10 },
                new Product { ID = 3, Name = "GreenCross", Price = 10 },

            };
            foreach (var item in products)
            {
               if (patterns.Contains(item.Name))
               {
                    Console.WriteLine(item.Name);
               }
            }

            Console.ReadLine();

1 个答案:

答案 0 :(得分:1)

patterns.Contains(item.Name)

为什么它会识别模式,你问的是项目名称是否包含在模式中。不是。您想询问项目名称中是否包含任何模式:

patterns.Any(item.Name.Contains)