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.
write below code on button click event.
C# Code for string compare
protected void Button1_Click(object sender, EventArgs e)
int result = string.Compare(str1, str2);
Label1.Text =”Result = “+ result.ToString();
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.
Here, str1 is grater than str2 then result would be grater than Zero means result is 1
In below screen str2 is grater than str1 so result is -1.