w3resource
C# Sharp Programming Exercises

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?



New Content: Composer: Dependency manager for PHP, R Programming