2015-04-06 49 views
0

我有這個需要識別具有模式符號的產品列表。到目前爲止,下面是我用來識別它們的代碼。不幸的是,當我調試程序時,它不識別模式。識別產品名稱中是否存在符號

  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
patterns.Contains(item.Name) 

它爲什麼會認識到模式,如果項目名稱包含在該模式你是問。不是。您想詢問產品名稱中是否包含任何圖案:

patterns.Any(item.Name.Contains) 
+0

謝謝!你幫了我 – Chris 2015-04-06 06:03:10