In this tutorial we will learn how to write a c# program to check whether a given substring is present in the given string
I have used Visual Studio 2019 Community for debugging purpose. But you can use any version of visual studio as per your availability.
C# Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
internal class Program { static void Main(string[] args) { 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(); } } |
Output:
1 2 3 4 5 |
Enter the string : Hello World Input the substring to search : World The substring exists in the string. |