String CompareTo Example to compare strings

 In this post we will see how to use String.CompareTo Method to match two strings. CompareTo Method returns following values:

  1. CompareTo Method returns. 
  2. Less than Zero.{when value of string with which we are compairing is greater than second value}. 
  3. Zero when strings match.
  4. Greater than Zero {when value of second string is greater than value with which we are compairing}.

Step 1: Take two Textboxes to receive input, one label to show output message and Button.

 
String1: 
String2: 
 

Step 2: Codebehind:

protected void btnCompare_Click(object sender, EventArgs e)
    {
        int a = txtInputValue1.Text.ToString().CompareTo(txtInputValue2.Text);
        lblResult.Text = a.ToString();

        if (a == 0)
        {
            lblResult.Text = "Strings entered are same.";
        }
        else
        {
            lblResult.Text = "Strings entered are not same.";
        }
    }

Final Output:

String-compare-example-asp-net-codingfusion

 

 
Asp.Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
8 + 8 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice