A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

How to check whether string contains substring or not in C#?
Q:

How to check whether string contains substring or not in C#?

0

Given a string and substring and we have to check whether a substring contains in a string or not using C#.Net.

string.Contains()

string.Contains() method returns true if given substring is exists in the string or not else it will return false.

Syntax:

bool string.Contains(string substring);

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
	class Program
	{
		static void Main()
		{
			string str = "Hello How are you? ";

			if (str.Contains("How") == true)
			{
				Console.WriteLine("Given string contains with in string");
			}
			else
			{
				Console.WriteLine("Given string does not contain with in string");
			}


			if (str.Contains("Who") == true)
			{
				Console.WriteLine("Given string contains with in string");
			}
			else
			{
				Console.WriteLine("Given string does not contain with in string");
			}
		}
	}
}

Output

Given string contains with in string
Given string does not contain with in string 

For first case: We are checking string (substring) "How" in the string str, the condition will be true because string str contains "How" but in other (second) case we are cheking "Who" which is not exists in string str i.e. string str does not contain the substring “Who”. Thus, condition will be false.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now