Contains Method Example in C#

Hi Programmers,welcome to new article of c#.net. This article i’ll write the program for String.Contains method in c# console application. Contains method is used to check substring occurs within string. If substring is found in string, it returns TRUE else FALSE. If value is the empty string empty(“”) then it also return TRUE.
Let’s see the codes.

If object reference set to an instance of an object. It generate an exception.

Directly Test Codes into Editor.

using System;

namespace ConsoleApp1 {
    class Program {       
        static void Main(string[] args) 
        {
            string str1 = "Hello World 65";
            string str2 = "Hello";
            string str3 = "Wor";
            string str4 = "";
            char ch = 'e';
            int k = 65;
            Console.WriteLine(str1.Contains(str2));
            Console.WriteLine(str1.Contains(str3));
            Console.WriteLine(str1.Contains(str4));
            Console.WriteLine(str1.Contains(ch));
            // Console.WriteLine(str1.Contains(k));
        }
    }
}

Happy Coding…Thanks

Post Author: adama