In the C# Contains() method is used to return a value indicating whether a specified character occurs within this string.The string Contains method is used to check whether the specified substring exists in the given string or not, and it will return a boolean value.
If a substring exists in a string, then the contains method will return true otherwise, it will return false.
C# String Contains Method Syntax
public bool Contains(string value)
In the given above systax, the Contains method will check whether the substring value exists or not, and it will return a boolean value.
Parameters
value: it is a string object which is used to check occurrence in the calling string.
Return
It returns boolean value either true or false.
C# String Contains Method Example
In the following example, using Contains() method to check whether the given value occurs within the string or not in the c# language.
using System;
public class StringContainsExample
{
public static void Main(string[] args)
{
string msg = "Hello Find and Solve";
string subtxt = "Solve";
Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt));
string subtxt1 = "Hello";
Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, msg.Contains(subtxt1));
Console.ReadLine();
}
}
output
Does Hello Find and Solve String Contains Solve?: True
Does Hello Find and Solve String Contains hello?: True
C# String Contains Case Insensitive
To perform a case-insensitive string comparison, you need to use the string IndexOf method. In the following example of performing a case insensitive search in the c# language.
Example
using System;
public class StringContainsExample
{
public static void Main(string[] args)
{ string msg = "Hello Find and Solve";
string subtxt = "Solve";
Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt, msg.Contains(subtxt));
string subtxt1 = "hello";
Console.WriteLine("Does {0} String Contains {1}?: {2}", msg, subtxt1, msg.Contains(subtxt1));
Console.ReadLine();
}
}
output
Does Hello Find and Solve String Contains Solve?: True
Does Hello Find and Solve String Contains hello?: False