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
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:
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.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework