C# String Contains()

The Contains() method checks whether the specified string is present in the string or not.

Example

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
   
      string str = "I love ice cream";  

// check if str contains "ice cream" bool check = str.Contains("ice cream");
Console.WriteLine(check); Console.ReadLine(); } } } // Output: True

Contains() Syntax

The syntax of the string Contains() method is:

Contains(String str, StringComparison comp)

Here, Contains() is a method of class String.


Contains() Parameters

The Contains() method takes the following parameters:

  • str - string which is to be checked
  • comp - ignores or considers case sensitivity

Contains() Return Value

The Contains() method returns:

  • True - if the string contains the specified substring
  • False - if the string doesn't contain the specified substring

Example 1: C# String Contains()

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
   
      string str = "I love ice cream";  

      bool check;

// check if str contains "ice cream" check = str.Contains("ice cream");
Console.WriteLine("contains ice cream: " + check);
// check if str contains "chocolate" check = str.Contains("chocolate");
Console.WriteLine("contains chocolate: " + check);
// returns true check = str.Contains("");
Console.WriteLine("contains \"\": " + check); Console.ReadLine(); } } }

Output

contains ice cream: True
contains chocolate: False
contains "": True

Here,

  • str.Contains("ice cream") - returns True as str contains "ice cream"
  • str.Contains("chocolate") - returns False as str does not contain "chocolate"
  • str.Contains("") - empty string always returns True

Example 2: Contains() With if...else

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
   
      string str = "Ice cream"; 
    
// checks if str contains "ice" if(str.Contains("ice"))
{ Console.WriteLine("str contains ice"); } else { Console.WriteLine("str does not contain ice"); } Console.ReadLine(); } } }

Output

str does not contain ice

In the above example, str.Contains("ice") returns False because the method Contains() is case sensitive.


Example 3: C# String Contains() Ignore Case

We can also ignore case sensitivity while using Contains().

using System;  
namespace CsharpString {  
  class Test {
    public static void Main(string [] args) {
   
      string str = "I love ice cream";  

      bool check;
   
// considers case sensitivity check = str.Contains("Ice cream", StringComparison.Ordinal);
Console.WriteLine("considers case sensitivity: " + check);
// ignores case sensitivity check = str.Contains("Ice cream", StringComparison.OrdinalIgnoreCase);
Console.WriteLine("ignores case sensitivity: " + check); Console.ReadLine(); } } }

Output

considers case sensitivity: False
ignores case sensitivity: True

Here,

  • StringComparison.Ordinal - considers the letter case
  • StringComparison.OrdinalIgnoreCase - ignores the letter case
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges