BlackWaspTM
C# Programming
.NET 1.1+

C# String Comparison Functions (2)

The twenty-third part of the C# Fundamentals tutorial continues the examination of the string manipulation functionality provided by the String class. This article investigates methods available for comparing the contents of strings.

Compare Method

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

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

result = String.Compare(animal1, "Cat");    // result is zero
result = String.Compare(animal2, "Cat");    // result is greater than zero
result = String.Compare(animal1, animal2);  // result is less than zero

Null Comparison

The CompareTo method raises an exception if the string that is being tested is null because a null object has no available methods. However, as the Compare method is static, null strings can be compared.

string animal = "Cat";
int result;

result = String.Compare(animal, null);      // result is greater than zero
result = String.Compare(null, animal);      // result is less than zero
result = String.Compare(null, null);        // result is zero

Case Sensitivity

The Compare method allows toy 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 user's language settings. To determine whether to use case-sensitive comparison a third, Boolean parameter is used. If the value is true, character casing is ignored. If false, the effect is the same as not supplying the parameter at all; the test is case-sensitive.

This example assumes UK English settings:

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

result = String.Compare(animal1, animal2, true);    // Strings are equal
result = String.Compare(animal1, animal2, false);   // Strings are not equal

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

CompareOrdinal Method

The CompareOrdinal static method allows case-sensitive comparison of two strings. It differs from Compare in that the comparison result is based upon the numeric Unicode values for each character. As the importance of characters in a cultural context can be different to the fixed numeric ordering of Unicode, different results may occur when comparing using this method. This is shown in the following example, which assumes a UK English configuration:

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

result = String.Compare(animal1, animal2);          // result is greater than zero
result = String.CompareOrdinal(animal1, animal2);   // result is less than zero

Equals Method

The Equals method is included for completeness. It returns a Boolean result indicating if two strings match, similar to the equality operator (==). The method can be used against a string object with a single parameter or as a static member with two parameters for the strings to be compared.

string animal = "Cat";
bool result;

result = animal.Equals("Cat");                      // result = true
result = animal.Equals("cat");                      // result = false
result = String.Equals(animal, "Cat");              // result = true
result = String.Equals(animal, "cat");              // result = false

.NET 2.0 IsNullOrEmpty Method

Sometimes it is necessary to check if a string is either empty or is undefined (null). Using .NET 1.1, this would require two tests. The .NET framework 2.0 introduces a new static method, named IsNullOrEmpty, which performs both tests with one statement. The method returns true when the string is empty or null.

string animal1 = "Cat";
string animal2 = "";
string animal3 = null;
bool result;

result = String.IsNullOrEmpty(animal1);             // result = false
result = String.IsNullOrEmpty(animal2);             // result = true
result = String.IsNullOrEmpty(animal3);             // result = true
5 December 2006