How to use string Contains method in C#

The string contains method is check if specified character is found in string or not.

The string contains method returns true/false, if string found in content of specified string then returns true other wise returns false.

Here, we tack an example to understand the concept of string contains method.

Here, we have two string str1 and str2, we check if the str2 value found in str1 the the result will be true, if str2 content is not found in str1 the result will be false.

First, design the asp.net web forms with two textbox control, a Button and a label control.
Here, we check if the content of string str2 is found/match with string str1 content then result will be true, if not then result will be false.

How to use string contains in C#

How to use string contains in C#

write below code on button control click event:

protected void Button1_Click(object sender, EventArgs e)
{
string str1=TextBox1.Text;
string str2 =TextBox2.Text;
Label1.Text = str1.Contains(str2).ToString();
}

we can see here the string1 value “Meera” and string2 value “Me”, the string2 value fond in the string1, so result is true.

How to use string contains in C#

How to use string contains in C#

Here, string2 value is not found in string1, so result is false.

How to use string contains in C#

How to use string contains in C#

Leave a Reply

Your email address will not be published. Required fields are marked *

9 + 8 =


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">