 .NET 1.1
C# String Comparison Functions
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.
Equality and Inequality Operators
In an earlier part of the C# Fundamentals tutorial we discussed the equality (==) and inequality (!=) relational operators. These operators allow an exact, case-sensitive comparison of two strings. In the case of the equality operator, if the strings match perfectly, the resultant Boolean value is true. If there is any difference between the two strings, the result is false. The inequality operator returns the opposite result. This was demonstrated with the following example:
string s1 = "String to compare.";
string s2 = "String to compare.";
string s3 = "String to Compare."; // Note the capital 'C'
bool result;
result = s1 == s2; // result = true
result = s1 == s3; // result = false
result = s1 != s2; // result = false
result = s1 != s3; // result = true
These two operators are useful in many situations but do have some limitations. They do not allow the programmer to consider the equality of two strings that match in all but capitalisation or other cultural factors. They also do not include any concept of ordering that would allow the comparison to indicate that one string is greater than another. This article explains the various string comparison methods that are provided by the string class to solve these problems.
Relative Comparison
CompareTo Method
The .NET framework provides a method named CompareTo for many data types including strings and numeric types. This method allows two values to be compared and returns a result that indicates not only if they are equal but also which value is the greater when they are not equal. The method considers cultural information when judging which of the strings are equal. For example, Japanese katakana characters can be written in two ways, half-width or full-width. The CompareTo method considers characters written as full-width to be equal to the same characters as half-width. The same cultural information is used to determine which of the strings is the greater.
The CompareTo method operates against an existing string. The string that it is to be compared against is passed as the only parameter. 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). |
This can be clarified with an example:
string animal1 = "Cat";
string animal2 = "Dog";
int result;
result = animal1.CompareTo("Cat"); // result is zero
result = animal2.CompareTo("Cat"); // result is greater than zero
result = animal1.CompareTo(animal2); // result is less than zero
NB: As this method is called against an existing string instance, if the string is null then an exception occurs:
string animal = null;
int result;
result = animal.CompareTo("Cat"); // Causes an exception
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 is zero
result = String.Compare(animal2, "Cat"); // result is greater than zero
result = String.Compare(animal1, animal2); // result is less than zero
Null Comparison
As described in the previous section, 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 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 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 assumes UK English local 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 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 the C# Fundamentals tutorial but are worth exploring. More information can be found at the MSDN definition of String.Compare web page.
CompareOrdinal Method
The next method for string comparison to be considered is the CompareOrdinal method. This is a static method of the string class that allows a case-sensitive comparison of two strings. It differs from the Compare method in that the comparison result is based upon the numeric Unicode values for each character compared. 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 that 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 of the string class is included in this article for completeness. This method returns a simple Boolean result indicating if two strings contain the same value. It provides similar functionality 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 of the string class, called IsNullOrEmpty, which performs both tests with a single line of code. The method simply returns a Boolean value that is true when the string is either 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
|