Check if the specified string is in the StringCollection in C#

To check if a specified string exists in a StringCollection, you use the Contains() method. The StringCollection class is part of the System.Collections.Specialized namespace and provides a collection specifically designed for storing strings.

Syntax

Following is the syntax for checking if a string exists in StringCollection −

bool result = stringCollection.Contains(string value);

Parameters

  • value ? The string to search for in the StringCollection.

Return Value

The Contains()true if the specified string is found in the StringCollection, otherwise false.

Example 1 ? String Not Found

This example demonstrates checking for a string that doesn't exist in the collection −

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 exist in the StringCollection? = " + stringCol.Contains("800"));
   }
}

The output of the above code is −

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

Example 2 ? String Found

This example demonstrates checking for a string that exists in the collection −

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 exist in the StringCollection? = " + stringCol.Contains("Katie"));
   }
}

The output of the above code is −

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

Example 3 ? Multiple String Checks

This example shows how to check multiple strings in a single program −

using System;
using System.Collections.Specialized;

public class Demo {
   public static void Main() {
      StringCollection stringCol = new StringCollection();
      stringCol.Add("Apple");
      stringCol.Add("Banana");
      stringCol.Add("Cherry");
      stringCol.Add("Date");
      
      Console.WriteLine("StringCollection contents:");
      foreach (string fruit in stringCol) {
         Console.WriteLine(fruit);
      }
      
      string[] searchItems = { "Apple", "Grape", "Cherry", "Mango" };
      Console.WriteLine("\nSearch results:");
      foreach (string item in searchItems) {
         bool found = stringCol.Contains(item);
         Console.WriteLine($"{item}: {found}");
      }
   }
}

The output of the above code is −

StringCollection contents:
Apple
Banana
Cherry
Date

Search results:
Apple: True
Grape: False
Cherry: True
Mango: False

Key Points

  • The Contains() method performs a case-sensitive comparison.

  • The search operation has O(n) time complexity where n is the number of elements.

  • StringCollection can contain duplicate strings, and Contains() will return true if at least one match is found.

Conclusion

The Contains() method in StringCollection provides a simple way to check if a specific string exists in the collection. It returns a boolean value indicating whether the string is found, making it useful for validation and search operations in string-based collections.

Updated on: 2026-03-17T07:04:36+05:30

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements