Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

C# Sharp Exercises: Check whether a given substring is present in the given string

C# Sharp String: Exercise-14 with Solution

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

C# Sharp Exercises: Check whether a given substring is present in the given string.

Sample Solution:-

C# Sharp Code:

using System;
public class exercise14
{
	public static void Main()
	{
  string str1,str2;
  bool m;
	
       Console.Write("\n\nCheck whether a given substring is present in the given strig :\n");
       Console.Write("-------------------------------------------------------------------\n");	
        Console.Write("Input the string : ");
               str1 = Console.ReadLine();	

	    Console.Write("Input the substring to  search : ");
               str2 = Console.ReadLine();
               m=str1.Contains(str2); // boolean value tapped hare
    if (m) // check boolean value is true or false.
		Console.Write("The substring exists in the string.\n\n");
	else
		Console.Write("The substring is not exists in the string. \n\n");
	}
}

Sample Output:

Check whether a given  substring is present in the given strig :       
-------------------------------------------------------------------    
Input the string : Welcome to w3resource.com                           
Input the substring to  search : w3resource                            
The substring exists in the string.

Flowchart:

Flowchart: Check whether a given substring is present in the given strig

C# Sharp Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C# Sharp to extract a substring from a given string without using the library function.
Next: Write a program in C# Sharp to read a sentence and replace lowercase characters by uppercase and vice-versa.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.