Comparing String Values

The < and > operators are not overloaded for the System.String type, which means that you can’t compare strings using the relational operators.
Instead. you can use the static System.String.Compare method, which takes two strings and returns an integer value.  If the first string is less than the second, a negative number is returned.  If the first string is greater, a positive number is returned.  If the strings are equal, the return value is zero.
1
2
3
4
int n = string.Compare("Sean", "Steinbeck");    // -1
n = string.Compare("Sean", "Bozo");             // 1
n = string.Compare("Sean", "Sean");             // 0
n = string.Compare("Sean", "sean");             // 1 ("S" > "s")
You can also use the CompareTo instance method of the string type:
1
int n = "Sean".CompareTo("Giotto");   // 1 (Sean > Giotto)
You can also ignore case during the comparison:


1
n = string.Compare("Sean", "sean", true);         // 0