C# String Contains Method

In c#, the string Contains method is used to check whether the specified substring exists in the given string or not, and it will return a boolean value.

 

If a substring exists in a string, then the contains method will return true otherwise, it will return false.

 

Following is the pictorial representation of checking whether the substring exists in a given string or not using the Contains() method in the c# programming language.

 

C# String Contains Method Representation Diagram

 

If you observe the above diagram, we are checking whether the “Welcome” text exists in the “Welcome to Tutlane” string or not using the Contains method, and it returns a true because the “Welcome” substring exists in the given string.

C# String Contains Method Syntax

Following is the syntax of defining a Contains method to check whether the substring exists in a given string or not in the c# programming language.

 

public bool Contains(string value)

If you observe syntax, the Contains method will check whether the substring value exists or not, and it will return a boolean value.

C# String Contains Method Example

Following is the example of using Contains() method to check whether the given value occurs within the string or not in the c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
           string msg = "Welcome to Tutlane";
           string subtxt = "Tutlane";
           Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt));
           string subtxt1 = "tutlane";
           Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, msg.Contains(subtxt1));
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
        }
    }
}

If you observe the above example, we used a Contains() method to determine whether the given substring value is found in string or not and returning a bool value.

 

When you execute the above c# program, you will get the result as shown below.

 

C# String COntains Method Example Result

 

If you observe the above result, the Contains() method performing a case sensitive operation that’s the reason for the “Tutlane” substring it returns “true” and for “tutlane” substring, it returns false.

C# String Contains Case Insensitive

To perform a case-insensitive string comparison, you need to use the string IndexOf method. Following is the example of performing a case insensitive search in the c# programming language.

 

using System;

namespace Tutlane
{
    class Program
    {
       static void Main(string[] args)
       {
           string msg = "Welcome to Tutlane";
           string subtxt = "Tutlane";
           Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt));
           string subtxt1 = "tutlane";
           StringComparison comp = StringComparison.OrdinalIgnoreCase;
           bool result = msg.IndexOf(subtxt1, comp) > 0 ? true : false;
           Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, result);
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
       }
    }
}

If you observe the above example, we used a string IndexOf method to perform a case insensitive search. We will learn more about the string IndexOf method in the next chapters.

 

When you execute the above c# program, you will get the result as shown below.

 

C# String Contains Case Insensitive Example Result

 

If you observe the above result, for both substrings “Tutlane” and “tutlane” we got a result as true.

 

This is how we can use Contains() method to check whether the specified substring exists in the given string or not in the c# programming language.