2020年3月17日 星期二

[C#] 字串與多個關鍵字是否符合

public static void Main()
{
   string[] conditions = new string[]{ "ABC", "DEF" };
  
   string target1 = "ABCXYZDEFIJK";  //符合 ABC DEF
   string target2 = "HGFPOIKJGLKHUNSD"; //完全不符合
   string target3 = "POIUYTREABC";   //符合 ABC
   string target4 = "MNBFDRGHDEFPOKHVGYV"; //符合 DEF

   var result1 = conditions.Any(x=>target1.Contains(x));
   Console.WriteLine("Target1 Result: {0}", result1);
  
   var result2 = conditions.Any(x=>target2.Contains(x));
   Console.WriteLine("Target2 Result: {0}", result2);
  
   var result3 = conditions.Any(x=>target3.Contains(x));
   Console.WriteLine("Target3 Result: {0}", result3);
  
   var result4 = conditions.Any(x=>target4.Contains(x));
   Console.WriteLine("Target4 Result: {0}", result4);
}

執行結果:
Target1 Result: True
Target2 Result: False
Target3 Result: True
Target4 Result: True

沒有留言:

張貼留言