BlackWaspTM
C# Programming
.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, the resultant Boolean value is true. If they are different the result is false. The inequality operator returns the opposite result, as shown below:

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 operators are useful but have limitations. They do not allow you to consider the equality of two strings that match in all but capitalisation or other cultural factors. Nor do they allow you to determine that one string is greater than another. This article explains the methods that are provided to solve these problems.

Relative Comparison

CompareTo Method

The .NET framework provides a method named CompareTo for many data types, including strings. It allows two values to be compared and returns a result that indicates not only if they are equal but also which is the greater when they are not. The method considers cultural information when comparing strings. 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 half-width characters. The same cultural information is used to determine which string is the greater.

The CompareTo method operates against an existing string. The string that it is to be compared with is passed to a parameter. The method returns an integer indicating the result of the comparison as follows:

Return ValueMeaning
ZeroThe strings are equal.
Less than ZeroThe first string is less than the second.
More than ZeroThe first is greater than the second or the second string is 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 an exception occurs:

string animal = null;
int result;

result = animal.CompareTo("Cat");           // Causes an exception
5 December 2006