String Compare Method in C#
Compare() – The C# Compare method compare two strings and returns integer value as output. It returns 0 for true and 1 and -1 for false.
The C# Compare function used for comparison of two string length.
Here, we have two string str1 and str2, we apply string compare function on strings then the result should be both strings are same, str1 is greater than str2, str2 is greater than str1. Here, we can get result in integer value Zero(0), grater than Zero(1), less than Zero(-1).
If both string are same = output result is 0
str1 is greater than str2 = output result is 1
str2 is grater than str1 = output result is -1
C# Example for String Compare()
Here, In above c# example both str1 and str2 are same, so result will be 0.
C# code for button click :
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();
}
Here, In above c# example both str1 is grater than str2, so result will be 1.
Here, In above c# example both str2 is grater than str1 so result will be -1.
Subscribe us
If you liked this c#.net post, then please subscribe to our YouTube Channel for more c#.net video tutorials.
We hope that this C#.net tutorial helped you to understand String Compare() Function.
Next asp.net tutorial we will learn about String Contains() Function.