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.


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.";
            lblResult.Text = "Strings entered are not same.";

Final Output:



Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

4 + 8 =

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