Introduction:

In this article we will explain how to use string Contains method or what is string Contains method in c# with example.

Description:

In c# programming language string Contains() method is a string method, the 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.


C# String Contains Method Syntax


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


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace SQLTutorialsPoint
{
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "Welcome to Sqltutorialspoint";
            string subtxt = "Sqltutorialspoint";
            Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt));
            string subtxt1 = "Sqltutorialspoint.com";
            Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, msg.Contains(subtxt1));
            Console.WriteLine("\nPress Enter Key to Exit.."); 
            Console.ReadLine();
        }
    }
}

 

as you see 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.

 how to use string Contains Method in C# with example

 

As you see above example, for both substrings “Sqltutorialspoint” and “Sqltutorialspoint.com” we got a result as true and second false.

 

In this article we have learned, how we can use Contains() method to check whether the specified substring exists in given  string or not in c# programming language.

 

 

I hope it will help to you after reading.

comments


Leave a comment

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

You may also like