判斷指定字串是否在 C# 中的 StringCollection 中


要檢查指定字串是否在 StringCollection 中,程式碼如下所示 −

示例

 即時演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "100", "200", "300", "400", "500" };
      Console.WriteLine("Array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);
      Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("800"));
   }
}

輸出

這會產生以下輸出 −

Array elements...
100
200
300
400
500
Does the specified string is in the StringCollection? = False

示例

我們來看另一個示例 −

 即時演示

using System;
using System.Collections.Specialized;
public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      String[] arr = new String[] { "John", "Tim", "Kevin", "Bradman", "Katie", "Tom", "Nathan" };
      Console.WriteLine("String array elements...");
      foreach (string res in arr) {
         Console.WriteLine(res);
      }
      stringCol.AddRange(arr);
      Console.WriteLine("Does the specified string is in the StringCollection? = "+stringCol.Contains("Katie"));
   }
}

輸出

這會產生以下輸出 −

String array elements...
John
Tim
Kevin
Bradman
Katie
Tom
Nathan
Does the specified string is in the StringCollection? = True

更新時間: 05-Dec-2019

70 次瀏覽

啟動您的 事業

完成本課程以獲得認證

開始
廣告
© . All rights reserved.