Write a C# program to check whether a given substring is present in the given string

Write a C# program to check whether a given substring is present in the given string

Introduction

I have used Visual Studio 2012 for debugging purpose. But you can use any version of visul studio as per your availability..

using System;
public class Stringexercise
{
    public static void Main()
    {
        string str1, str2;
        bool m;
 
        Console.Write("Enter the string : ");
        str1 = Console.ReadLine();
 
        Console.Write("Input the substring to  search : ");
        str2 = Console.ReadLine();
        m = str1.Contains(str2); 
        if (m) // check boolean value is true or false.
            Console.WriteLine("The substring exists in the string.");
        else
            Console.WriteLine("The substring is not exists in the string.");
 
        Console.ReadLine();
    }
}

Result

Write a C# program to check whether a given substring is present in the given string
Write a C# program to check whether a given substring is present in the given string

Add a Comment

Your email address will not be published. Required fields are marked *