C# string compare example

In this asp .net tutorial we will learn how to use c# string compare method with example and source code. C# string compare method is used to compare two strings, you have option to ingnore or compare case of the string. C# string method returns a result that provides us information that string is greater than or less than the other, or that the two strings are equal.

Meaning of values returned by C# string compare method:

Value

Condition

Less than zero

This means that strA is smaller than strB in the sort order.

Zero

This means strA and strB are in same position in the sort order.

Greater than zero

This means strA is greater than strB in the sort order.

C# string compare method:

        public static int Compare(
	               string strA,
	               string strB,
	               bool ignoreCase
        )       
    
Here strA and strB are two strings you want to compare and ignoreCase is Condition [Whether you can to compare case or not]. If you want to compare case sensitiviy then set this value to false else to ignore case set true.

HTML markup:

        
String1:
String2:

C# string compare with case example:

      protected void btnCompare_Click(object sender, EventArgs e)
      {
      int result = string.Compare(txtInputValue1.Text, txtInputValue2.Text, false);
      lblResult.Text = result.ToString();
      }
    

C# string compare without case example:

      protected void btnCompare_Click(object sender, EventArgs e)
      {  
      int result = string.Compare(txtInputValue1.Text, txtInputValue2.Text, true);
      lblResult.Text = result.ToString();
      }
    
 
About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice