Introduction
If you want to compare two string object then we can use compare( ) method of the string class. Using this method you can compare characters through casing rules and the alphabetic order. It returns 32-bit signed integer value. If returning value is equal to zero then both string are equal otherwise string is greater or less.Suppose, your string returns less than zero value it means your first string is less than to string b.
Lets take an simple example
Source code<div>
<asp:Button ID="Button1" runat="server" Text="String Compare" onclick="Button1_Click" />
<br />
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
</div>
Business logic code
#region stringcompare
protected void Button1_Click(object sender, EventArgs e)
{
string a = "hello";
string b = "hello";
int c = string.Compare(a, b);
if (c == 0)
{
Label1.Text = "Both string are equal";
}
else
{
if (c < 0)
{
Label1.Text = "string a is less than string b";
}
else
{
Label1.Text = "string a is greater than string b";
}
}
}
#endregion
Code generate the following output