In c#, the string Compare method is used to compare two specified strings and return an integer value that indicates their relative position in the sort order.
Here, the return integer value indicates the lexical relationship between the two specified strings. The following table lists the values that will be returned by the Compare method after comparing the two specified strings.
Condition | Value |
---|---|
strA == strB | 0 |
strA > strB | 1 |
strA < strB | -1 |
Following is the pictorial representation of comparing two strings using Compare() method in c# programming language.
If you observe the above diagram, we are comparing two strings,”Suresh” and “Dasari” by using the Compare method and return an integer value “1”.
Following is the syntax of defining a Compare method to compare two specified strings in the c# programming language.
If you observe syntax, the Compare method will compare a given two strings and return an integer value.
Following is the example of using the Compare() method to compare a given two strings and return an integer value in the c# programming language.
If you observe the above example, we used a Compare() method to compare two given strings and return an integer value.
When you execute the above c# program, you will get the result as shown below.
This is how you can use Compare() method to compare two given strings and return an integer value which indicates a lexical relationship between two strings in the c# programming language.