Brian's Blog

items I see across my tribes

C# String.Compare

March 13
by briancarter 13. March 2010 08:45

When comparing strings, you have several options.  One of the more optimal methods which offers more options than a simple “==”, is the String.Compare method. 

The Compare function is a static method of the string class. This method provides similar functionality to CompareTo but allows more options to be specified. As a static member of the string class, the two strings to be compared are both passed as parameters.

string animal1 = "Cat";
string animal2 = "Dog";
int result;

result = String.Compare(animal1, "Cat"); // result 0
result = String.Compare(animal2, "Cat"); // result > 0
result = String.Compare(animal1, animal2); // result < 0

The compare method operates against two existing string. The method returns an integer indicating the result of the relative comparison as follows:

Return Value Meaning
Zero The two strings are equal.
Less than Zero The first string has a value that is less than the second.
More than Zero The first string has a value that is greater than the second or the second string is undefined (null).

Null Comparison

The CompareTo method raises an exception if the string that is being tested is null. This is because the null object assigned to the string has no available methods. However, as the Compare method is static, and therefore does not require an object instance, null strings can be compared.

string animal = "Cat";
int result;

result = String.Compare(animal, null); // result > 0
result = String.Compare(null, animal); // result < 0
result = String.Compare(null, null);   // result = 0
Case Sensitivity

The Compare method allows the programmer to decide whether to perform case-sensitive or case-insensitive comparisons. The differentiation between upper case and lower case lettering is applied according to the rules of the user's local language settings. To determine whether to use case-sensitive comparison a Boolean value is supplied as a third parameter. If the value is true then character casing is ignored. If the parameter is set to false then the the effect is the same as not supplying the parameter at all; the test is case-sensitive.

This example:

string animal1 = "Cat";
string animal2 = "cat"; // Note use of lower case
int result;

result = String.Compare(animal1, animal2, true);  // result = 0
result = String.Compare(animal1, animal2, false); // result != 0

The Compare method includes several other overloaded variations allowing the comparison of strings using further control of international language settings, as well as facilities for comparing only parts of strings. These are beyond the scope of this blog but are worth exploring. More information can be found at the MSDN definition of String.Compare web page.

Categories: Development


 Questions or Feedback, my contact information is located on my About page.


The opinions, thoughts, and comments made in these blog posts are solely my own (unless otherwise stated). They do not reflect the opinions, thoughts or practices of my employer, my universities, my family, or anyone else. Also, I retain the right to change my mind about anything I publish here without having to go back and edit posts that occurred in the past. 

These are my opinions, or just as likely, someone else's opinions that I leveraged for my own.