Article C0009 C# .NET
String comparison

During the creation of some of my tools I found out that the comparison behavior of the available controls are different. When you compare the text value of a textbox with a value from a listbox, the comparison is case-insensitive. This can result in unexpected sorting or worse, incorrect functioning business-logic. Let’s start with the examination of available ways to compare strings.

.CompareTo()
The CompareTo method is supported for most of the available data types. The following example is going to use the string data type and explains the methods behavior.

    int result;
    string car_a = "Volvo";
    string car_b = "Audi";
    result = car_a.CompareTo(car_b);	// result = 1
    result = car_a.CompareTo("Volvo");	// result = 0
    result = car_a.CompareTo("volvo");	// result = 1
    result = car_b.CompareTo(car_a);	// result = -1
When the CompareTo method is used against a null string, an exception is raised. So prior using the comparison challenge the variable like this: if (car_a != null) …

Return value
The following table shows the return values of this method:

.CompareTo()
Return valueComment
Less than zerocar_a precedes car_b in the sort order.
Zerocar_a has the same position in the sort order as car_b.
Greater than zerocar_b follows car_b in the sort order of car_b is null.


.Compare()
This method is available for the string data type and supports more functionality than the previously mentioned CompareTo method.

    int result;
    string car_a = "Mercedes";
    string car_b = "Hummer";
    result = String.Compare(car_a, "Mercedes");	// result = 0
    result = String.Compare(car_a, "mercedes");	// result = 1
    result = String.Compare(car_a, car_b);	// result = 1
    result = String.Compare(car_b, car_a);	// result = -1
Return value
The following table shows the return values of this method:

.Compare()
Return valueComment
Less than zerocar_a is less than car_b.
Zerocar_a equals car_b.
Greater than zerocar_a is greater than car_b.


Case Sensitive
One of the benefits of using the Compare method is the fact that you are able to decide if the comparison needs to be case sensitive or not.

    int result;
    string car_a = "Lexus";
    string car_b = "Hyundai";
    result = String.Compare(car_a, car_b, true);	// result = 1
    result = String.Compare(car_a, car_b, false);	// result = 1
    result = String.Compare(car_b, car_a, true);	// result = -1
    result = String.Compare(car_b, car_a, false);	// result = -1
    result = String.Compare(car_a, "Lexus", true);	// result = 0
    result = String.Compare(car_a, "Lexus", false);	// result = 0
    result = String.Compare(car_a, "lexus", true);	// result = 0
    result = String.Compare(car_a, "lexus", false);	// result = 1
The Compare method is a static method and can be used to challenge a value against a null string without raising an exception. This way a string can be tested using the following code:

    result = String.Compare(car_a, null);	// result = 1
    result = String.Compare(null, car_a);	// result = -1
Return value
The following table shows the return values of this method:

.Compare() case-sensitive overload
Return valueComment
Less than zerocar_a is less than car_b.
Zerocar_a equals car_b.
Greater than zerocar_a is greater than car_b.


.CompareOrdinal()
The CompareOrdinal method is a static method which van be used for case-sensitive string comparison. The difference with the Compare method lies in the fact that the CompareOrdinal methods result is based upon the numeric Unicode values for each compared character.

    int result;
    string car_a = "scoda";
    string car_b = "Scoda";
    result = String.Compare(car_a, car_b);		// result = -1
    result = String.Compare(car_b, car_a);		// result = 1
    result = String.CompareOrdinal(car_a, car_b);	// result = 32
    result = String.CompareOrdinal(car_b, car_a);	// result = -32
This method can be used when comparison within a cultural context environment is required. The return value, System.Int32, indicates the lexical relationship between the two strings.

Return value
The following table shows the return values of this method:

.CompareOrdinal()
Return valueComment
Less than zerocar_a is less than car_b.
Zerocar_a equals car_b.
Greater than zerocar_a is greater than car_b.


.Equals()
Although the Equals method is just an equivalent of the == operator, it can also be used for comparison. The Equals method always return a Boolean value. Due to this limitation the Equals method is not very useful within a sort routine.

    bool result;
    string car_a = "Peugeot";
    result = car_a.Equals("peugeot");		// result = false
    result = car_a.Equals("Peugeot");		// result = true
    result = String.Equals(car_a, "peugeot");	// result = false
    result = String.Equals(car_a, "Peugeot");	// result = true
The result is true when the compared strings are exactly the same otherwise the result will be false.

.IsNullOrEmpty
The IsNullOrEmpty method is introduced within the .NET 2.0 Framework. The method will return a Boolean value of true when the string is either empty or null.

    bool result;
    string car_a = "Seat";
    string car_b = "";
    string car_c = null;
    result = String.IsNullOrEmpty(car_a);	// result = false
    result = String.IsNullOrEmpty(car_b);	// result = true
    result = String.IsNullOrEmpty(car_c);	// result = true
Source code
The source code which I’ve used to examine the different behavior can be downloaded here.

~Edward