C# String Contains Method

  Keywords : string contains c#, c# contains example, c# if string contains, csharp string contains example, c# string contains substring, c# array or list contains, c# contains method example

Here we will learn string contains method in c# with examples and how to use c# string contains method to check whether the specified substring exists in string or not with examples.

C# String Contains Method

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

 

In case, if substring exists in 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 given string or not using Contains() method in c# programming language.

 

C# String Contains Method Representation Diagram

 

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

C# String Contains Method Syntax

Following is the syntax of defining a Contains method to check whether the substring exists in given string or not in 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 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, subtxt, msg.Contains(subtxt1));

            Console.WriteLine("\nPress Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

If you observe 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 we execute above c# program, we will get the result like as shown below.

 

C# String COntains Method Example Result

 

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

C# String Contains Case Insensitive

To perform case-insensitive string comparison, then we need to use string IndexOf method. Following is the example of performing a case insensitive search in 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 Any Key to Exit..");

            Console.ReadLine();

        }

    }

}

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

 

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

 

C# String Contains Case Insensitive Example Result

 

If you observe 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 given string or not in c# programming language.