How to use string Compare method in C#

We use string Compare function for comparison of two string length.  In string compare function we get three types of out put in result. suppose we have two string str1 and str2, we apply string compare function on string then we get result like both string are same, str1 is greater than str2, str2 is greater than str1.

Here, we can get result in integer value Zero, grater than Zero, less than Zero

If both string are same =output result is Zero

str1 is greater than str2 = output result is grater than Zero

str2 is grater than str1 = output result is less than Zero

lets, take an example of string compare.

Create asp.net web application and design a web forma with two textbox, one Button along with a Label control.

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

write below code on button click event.

C# Code for string compare

 protected void Button1_Click(object sender, EventArgs e)
{
string str1=TextBox1.Text;
string str2=TextBox2.Text;
int result = string.Compare(str1, str2);
Label1.Text =”Result = “+ result.ToString();
}

VB.Net Code

Dim str1 As String = TextBox1.Text
Dim str2 As String = TextBox2.Text
Dim result As Integer =  String.Compare(str1,str2)
Label1.Text =”Result = “+ result.ToString()

 

In below screen the both str1 and str2 are same, so result would be Zero.

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

 

Here, str1 is grater than str2 then result would be grater than Zero means result is 1

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

 

In below screen str2 is grater than str1 so result is -1.

How to use string compare method in asp.net C#

How to use string compare method in asp.net C#

Leave a Reply

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

5 + 3 =


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="">